Skip to content
Crayon CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

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

The story

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.

Play with it

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

  1. 1home = (51.5, -0.12)
  2. 2lat, lon = home
  3. 3a, b = "tea", "coffee"
  4. 4a, b = b, a
  5. 5nums = [3, -1, 4, -5, 2]
  6. 6squares = [n * n for n in nums if n > 0]
  7. 7print(squares)

➜ yellow = the line about to run · ✓ = the line that just ran

Name it

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, then lo, 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.

Build it

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.

Challenge

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.

Remember it

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?