Topic 03

Tuples and Sets

Hands-on

Lists and dictionaries do most of the work, but two smaller tools complete the everyday kit, each with one clear job. A tuple is a small, fixed group of values that belong together, such as a pair of coordinates. A set is a collection of unique values, the right tool for "have I seen this before?".

Think of a tuple as a sealed pack of three batteries: the pack is one thing, and you cannot swap one battery out. A set is like a guest book where each name is written only once, however many times that guest visits.

Tuples

A tuple is written in round brackets, with commas between the values. You read items by position, exactly as with a list. What you cannot do is change it: assigning to a position is a TypeError, because a tuple does not support item assignment. That fixedness is the point. A pair of coordinates, a date split into year, month and day, a name and its score: things whose parts belong together and should not drift apart.

A tuple can be read but not changed
>>> position = (48.86, 2.35)
>>> position[0]
48.86
>>> position[0] = 50.0
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    position[0] = 50.0
    ~~~~~~~~^^^
TypeError: 'tuple' object does not support item assignment

Unpacking

Unpacking hands the values of a tuple, or a list, to several names at once. latitude, longitude = position gives the first value to the first name and the second to the second. The number of names must match the number of values. This is exactly what happened in the last topic, where each pair from items was unpacked into a name and a number.

Unpacking a pair into two names
>>> latitude, longitude = position
>>> latitude
48.86
>>> longitude
2.35

Sets

A set holds each value at most once. Write it in curly braces without colons, or turn a list into a set with set. Duplicates disappear, which makes a set the quickest way to find the distinct values in a list. Checking whether a value is in a set with in is instant, even for a set of a million items. A set keeps no particular order, so do not rely on the order it prints in.

Duplicates disappear in a set
>>> visitors = ["Anna", "Omar", "Anna", "Lena", "Omar"]
>>> unique = set(visitors)
>>> len(unique)
3
>>> "Lena" in unique
True

One Trap: Empty Braces

A pair of empty curly braces makes an empty dictionary, not an empty set, because dictionaries were in Python first. To make an empty set, write set() with nothing in the brackets.

Empty braces are a dictionary
>>> type({})
<class 'dict'>
>>> type(set())
<class 'set'>

Choosing

A list when you have an ordered collection that changes. A tuple when you have a fixed group of values that belong together. A set when what matters is whether something is present, and each thing counts once. A dictionary when you look things up by name.

Which container for which job
You needUseExample
An ordered collection that changeslista shopping list
A fixed group that belongs togethertuple(latitude, longitude)
Each value once, fast "is it here?"setvisitors seen today
Look up by namedictionaryphone numbers by person
Common Confusions
  • "Tuples are just lists with round brackets." They cannot be changed after they are made, and that is exactly why you choose one.
  • "A set keeps the order I added things in." Sets promise no order at all. Use a list when order matters.
  • "{} makes an empty set." It makes an empty dictionary. Write set() for an empty set.
Why It Matters
  • Unpacking makes code that handles pairs and records read naturally, and it appears in almost every loop over a dictionary.
  • Removing duplicates and checking membership are everyday data jobs a set does in one line.

Knowledge Check

What happens with position[0] = 50.0 when position is a tuple?

  • The first value in the tuple changes to 50.0
  • A new value is added at the front of the tuple
  • A TypeError, because tuples do not support item assignment
  • The tuple quietly turns into a list, then changes

After a, b = (3, 7), what are a and b?

  • a is 3 and b is 7, in that order
  • a is 7 and b is 3, the reverse order
  • Both are the whole tuple (3, 7)
  • a is (3, 7), and b is left empty

What does set(["Anna", "Omar", "Anna"]) contain?

  • Three values, with Anna appearing twice
  • Two values: Anna and Omar, one of each
  • One value, only the first name in the list
  • The list itself, unchanged, inside the set

What does {} create?

  • An empty set, with room for unique values
  • An empty tuple that cannot be changed
  • An empty list with nothing in it yet
  • An empty dictionary, with no keys

You got correct