Python Mastery · Chapter 3 · ~18 min
Lists, Dicts, Sets & Tuples Visualised
Python's four containers, explained with one real story: you run a small online shop. Carts, price lists, delivery cities and shop location each need a different container — you'll see why.
- Know which container fits which job
- See how each one stores data
- Avoid the 4 classic beginner traps
- Turn a CSV/Excel sheet into Python data
1Meet the four containers
Think of them as four kinds of storage in your shop:
[ ] list, ( ) tuple, { } with single values = set, { key: value } = dict.2Play with them
Switch tabs and press the actions. Watch what changes — and which actions Python refuses.
🛒 A shopping cart: items in order, you can add or remove, duplicates allowed.
cart =
Pick an action below.
What does this print?
cart = ["pen", "book"]
cart.append("pen")
print(len(cart))3Why sets and dicts are so fast
A list is checked one item at a time. Sets and dicts use a hash: Python turns the value into a number that says exactly which bucket to open. Try both searches.
List: checks one by one
Set: hash → jump to bucket
TypeError: unhashable type: 'list' — its hash could change later.You check 'is this email already registered?' 100,000 times. Best container for emails?
4The 4 classic traps
Trap 1 — the one-item tuple. The comma makes a tuple, not the brackets.
What is type(x)?
x = (42)
Trap 2 — removing items while looping. Step through to see an item escape:
msgs = ["spam", "spam", "hi", "spam"]
for m in msgs:
if m == "spam":
msgs.remove(m) # ⚠️ changing while loopingLoop starts at position 0: 'spam' → remove it.
Trap 3 — missing keys. prices["sofa"] crashes with KeyError. Use prices.get("sofa", 0) to get a safe default.
Trap 4 — the empty set. {} is an empty dict. An empty set is written set().
Which creates an EMPTY SET?
5Real data: CSV & Excel become Python
Real work starts with a spreadsheet. When Python (or pandas) reads it, each container has a clear job:
| customer | city | amount |
|---|---|---|
| Asha | Delhi | 450 |
| Ravi | Pune | 1200 |
| Asha | Delhi | 300 |
orders = [
{"customer": "Asha", "city": "Delhi", "amount": 450},
{"customer": "Ravi", "city": "Pune", "amount": 1200},
{"customer": "Asha", "city": "Delhi", "amount": 300},
] # csv.DictReader gives you exactly thisOpen your own data in the CSV Viewer & Editor or XLSX Studio, then picture every row as a dict.
6Challenge: design the shop's data
Pick the best container for each real shop need. Get all five right to complete the chapter.
1. Today's orders, in the order they arrived (a customer may order twice)
2. Unique customers who bought something today
3. Product name → stock remaining
4. The shop's fixed GPS position (lat, lng)
5. Pin codes we deliver to (checked on every checkout)
🎯 0 / 5 correct
Now run a real shop report — counting unique customers, revenue per city and even a pandas bonus — in the Python Playground (code prefilled).
7Recap
- List = ordered & changeable (cart). Tuple = ordered & locked (GPS).
- Set = unique items, instant “is it in?” (delivery cities).
- Dict = key → value, instant lookup (price list).
- Use
(x,)for one-item tuples,set()for empty sets,.get()for safe lookups. - Don't remove from a list while looping it — build a new list instead.
- A spreadsheet is a list of dicts; a unique column is a set.
Finish the challenge above to complete the chapter, or