Lesson 15 of 26 · about 14 min
Tuples and list comprehensions
A tuple is a list that can't change, perfect for fixed groups like coordinates. A list comprehension builds a whole list in one readable line.
🧭 What you need first: Mutability, aliasing and cloning
Some groups of values belong together forever: the latitude and longitude of your house, a date (day, month, year), the red-green-blue amounts of a color. You’d never want to append a fourth number to a GPS location by accident.
For those, Python has the tuple: a list that’s sealed. Same indexing, but no changes allowed. That’s a feature: if nothing can change it, nothing can break it.
And for building lists, Python has a shortcut that reads almost like English: “give me n squared for each n in my numbers if n is positive.” That’s a list comprehension.
Another way: a printed receipt vs. a shopping list
A shopping list (list) gets edited all the time. A receipt (tuple) is a fixed record: date, total, store. Nobody edits a receipt.
Another way: a sandwich order
A comprehension is like ordering: “One toasted version of each bread on the menu, except rye.” You describe the result you want, instead of listing each step.
Unpack, swap and build
Step through. Watch line 3 unpack a tuple into two names, line 4 swap them in one move, and line 6 build a whole list at once.
Watch the code run, one step at a time
- 1home = (51.5, -0.12)
- 2lat, lon = home
- 3a, b = "tea", "coffee"
- 4a, b = b, a
- 5nums = [3, -1, 4, -5, 2]
- 6squares = [n * n for n in nums if n > 0]
- 7print(squares)
➜ yellow = the line about to run · ✓ = the line that just ran
The real words
Tuples
- A tuple is an immutable sequence:
(1, 2, 3). Parentheses are optional when it’s clear:t = 1, 2, 3. - A one-item tuple needs a trailing comma:
(5,). Without it,(5)is just the number 5 in brackets. - Use tuples for fixed-size records (coordinates, RGB colors, dates) and as dictionary keys (lesson 17), since lists can’t be keys.
- Unpacking:
x, y = (3, 4). The number of names must match the number of items. - A function can return several values as a tuple:
return smallest, largest, thenlo, hi = min_max(nums).
List comprehensions
result = [expression for item in sequence if condition]is a compact version of:
result = []
for item in sequence:
if condition:
result.append(expression)[x * 2 for x in range(5)]→[0, 2, 4, 6, 8][w.upper() for w in words]→ every word in capitals[w for w in words if len(w) > 3]→ only the long words
Use comprehensions for simple transform-and-filter jobs. If it doesn’t fit comfortably on one line, a normal loop is clearer, and clear beats clever.
Smallest, largest, and a filter
Write min_max(nums) that returns a tuple (smallest, largest). Then use a list comprehension to set even_squares to the squares of the even numbers in nums.
Clean up messy data
Survey answers came in messy. Use ONE list comprehension to make clean: each answer stripped of spaces and lowercased, skipping any that are empty after stripping.
What does [len(w) for w in ['hi', 'hello', 'hey']] give?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?