// python · beginner

Python Tuples and Sets Explained (and When to Use Them)

Learn what Python tuples and sets are, how they differ from lists, tuple unpacking, removing duplicates with sets, set operations, and how to choose the right collection.

You already know lists and dictionaries. Python has two more built-in collections that beginners often skip: tuples and sets. They’re simpler than they sound, and each one is the perfect tool for certain jobs.

Tuples: lists that can’t change

A tuple looks like a list, but uses round brackets:

python
point = (3, 5)
colours = ("red", "green", "blue")

You read items from a tuple exactly as you would from a list:

python
print(colours[0])     # red
print(colours[-1])    # blue
print(len(colours))   # 3
print("green" in colours)   # True

The big difference is that tuples are immutable: once created, they can’t be changed.

python
colours[0] = "pink"
# TypeError: 'tuple' object does not support item assignment

There’s no append, remove or sort either.

Why would you want something that can’t change?

  • Safety. If a value should never change, such as the days of the week or a fixed screen size, a tuple makes accidental changes impossible.
  • Meaning. A tuple usually says “these values belong together as one thing”, like the (x, y) of a point or the (red, green, blue) of a colour.
  • Dictionary keys. Tuples can be used as dictionary keys; lists can’t.
python
seats = {(1, 1): "Ava", (1, 2): "Leo"}   # (row, seat) -> name
print(seats[(1, 2)])   # Leo

The one-item tuple trap

Brackets alone don’t make a tuple; the comma does:

python
a = (5)
b = (5,)
print(type(a))   # <class 'int'>
print(type(b))   # <class 'tuple'>

Tuple unpacking

This is where tuples really shine. You can split a tuple into separate variables in one line:

python
point = (3, 5)
x, y = point
print(x)   # 3
print(y)   # 5

It’s how Python lets a function return more than one value: it returns a tuple, and you unpack it.

python
def min_and_max(numbers):
    return min(numbers), max(numbers)

low, high = min_and_max([4, 9, 1, 7])
print(low, high)   # 1 9

And it gives you a neat way to swap two variables:

python
a = 1
b = 2
a, b = b, a
print(a, b)   # 2 1

You’ve also been using unpacking already in loops such as for key, value in d.items():, where each item is a tuple.

Sets: unique items, no order

A set is a collection of unique values, written with curly braces:

python
languages = {"Python", "SQL", "Python", "HTML"}
print(languages)   # {'SQL', 'HTML', 'Python'} (order may vary)
print(len(languages))   # 3

The duplicate "Python" disappeared. Sets have two key properties:

  1. No duplicates: each value appears at most once.
  2. No order: there are no indexes, so languages[0] is an error.

To make an empty set, you must use set(), because {} makes an empty dictionary:

python
seen = set()
print(type({}))      # <class 'dict'>
print(type(seen))    # <class 'set'>

Adding, removing and checking

python
tags = {"python", "beginner"}
tags.add("loops")
tags.add("python")       # already there, so nothing changes
tags.discard("beginner") # removes it (no error if it's missing)
print("loops" in tags)   # True

Checking whether something is in a set is extremely fast, even for millions of items, which makes sets great for “have I seen this before?” checks.

Removing duplicates from a list

The most common use of a set in beginner code:

python
emails = ["a@x.com", "b@x.com", "a@x.com", "c@x.com"]
unique = set(emails)
print(len(unique))   # 3

If you need the result as a sorted list again:

python
print(sorted(unique))   # ['a@x.com', 'b@x.com', 'c@x.com']

Set operations

Sets can be combined just like in maths:

python
python_club = {"Ava", "Leo", "Mia"}
art_club = {"Mia", "Sam", "Leo"}

print(python_club & art_club)   # in both: {'Leo', 'Mia'}
print(python_club | art_club)   # in either: all four names
print(python_club - art_club)   # only in Python club: {'Ava'}
print(python_club ^ art_club)   # in one but not both: {'Ava', 'Sam'}
Operator Method Meaning
a & b a.intersection(b) items in both
a | b a.union(b) items in either
a - b a.difference(b) items in a but not b
a ^ b a.symmetric_difference(b) items in exactly one

Choosing the right collection

You need… Use
an ordered collection you’ll change list [ ]
a fixed group of values that belong together tuple ( )
unique values, or fast membership checks set { }
to look values up by a name or key dictionary {key: value}

Mini project: a word-game helper

python
def letters_in_common(word1, word2):
    return sorted(set(word1.lower()) & set(word2.lower()))

def has_repeated_letters(word):
    return len(set(word.lower())) < len(word)

first = input("First word: ").strip()
second = input("Second word: ").strip()

shared = letters_in_common(first, second)
print(f"Shared letters: {', '.join(shared) or 'none'}")

for w in (first, second):
    if has_repeated_letters(w):
        print(f"'{w}' has repeated letters.")

For python and typing, the shared letters are n, p, t, y. has_repeated_letters works because turning a word into a set removes repeated letters, so the set is shorter than the word. Challenge: print the letters that appear in the first word but not the second.

Key points

  • Tuples ( ) are like lists that can’t change; the comma makes the tuple.
  • Unpacking (x, y = point) assigns several variables at once.
  • Sets { } hold unique values with no order; use set() for an empty set.
  • set(my_list) removes duplicates; in checks are very fast.
  • &, |, - and ^ find common, combined and different items.

Next: label your data with keys in Python Dictionaries Explained.