3. Lists, Tuples, Sets & References

Tuples, Sets & List Shortcuts (Comprehensions)

Learn when to lock data with tuples, unpack multiple variables in one line, remove duplicates automatically with sets, and transform lists cleanly.

3 min readUpdated 2026-11-29
In This Lesson
  • Lock fixed values in tuples and unpack them into separate variables
  • Remove duplicates automatically and test membership quickly with sets
  • Transform and filter lists concisely using list comprehensions

Lists can do almost anything, so why does Python have tuples and sets too? Because each one solves a very common problem effortlessly:

  • Tuple: "Keep this list locked so nobody accidentally changes it."
  • Set: "Automatically throw away any duplicate items."

Tuples (Locked Lists) & Unpacking

A tuple looks just like a list, except it uses round parentheses (...) instead of square brackets [...]:

python
screen_size = (1920, 1080)
print(screen_size[0])   # 1920
print(screen_size[1])   # 1080

If someone tries screen_size[0] = 800, Python stops them with a TypeError—protecting values that shouldn't change, like screen dimensions or map coordinates.

Bonus Trick: Unpacking into Separate Variables

Instead of writing width = screen_size[0] and height = screen_size[1], you can unpack both in one clean line:

python
width, height = (1920, 1080)
print(f"Width is {width}, height is {height}")

Sets (Duplicate-Free Collections)

Suppose 5 people sign up for a newsletter, and two people accidentally typed the same email:

python
emails = ["alex@mail.com", "sam@mail.com", "alex@mail.com"]
unique_emails = set(emails)

print(unique_emails)   # {'alex@mail.com', 'sam@mail.com'}

A set uses curly braces {...} and enforces two rules:

  1. No duplicates allowed (Python silently drops repeats).
  2. No position numbers (a set is like a bag of marbles—items don't have a fixed order, so unique_emails[0] won't work).

You can add items with .add(...) and check if something is inside super fast with in:

python
unique_emails.add("maya@mail.com")
print("sam@mail.com" in unique_emails)  # True
Careful Creating an Empty Set

Writing {} creates an empty dictionary (which we'll learn soon), NOT an empty set! Always write my_set = set() when starting empty.

A Clean Shortcut for Building Lists

Suppose you have a list of prices, and you want a new list of only the prices under $50, each doubled.

Here is the normal for loop way:

python
prices = [10, 80, 25, 60]
doubled = []

for p in prices:
    if p < 50:
        doubled.append(p * 2)

print(doubled)   # [20, 50]

Python lets you write those exact 4 lines as a single readable list comprehension:

python
doubled = [p * 2 for p in prices if p < 50]

Read it left to right in 3 simple chunks:

  1. p * 2 → what to put in the new list
  2. for p in prices → loop through prices
  3. if p < 50 → (optional) keep only items passing this test

Try It Yourself in the Playground

Python 3Try It Live: Tuples, Sets & List Shortcuts
Output

Click “Run Code” to execute this snippet right in your browser.

Mini-Challenge: Use a list comprehension to clean names = [" maya ", "ALEX", " sam "] so every name has extra spaces removed and starts with a capital letter (["Maya", "Alex", "Sam"]). (Hint: use .strip().title().)

Quick Checkpoints

Check Your Understanding

What is the main difference between a list [1, 2] and a tuple (1, 2)?

Check Your Understanding

What does [n + 1 for n in [10, 20, 30]] produce?