Chapter 3 of 13 · Free, no signup

Support Us

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

["pen", "book", "pen"]

A shopping cart: items in order, you can add or remove, duplicates allowed.

Tuple

(28.61, 77.20)

A delivery location: latitude and longitude that must never change.

Set

{"Delhi", "Mumbai"}

Cities you ship to: each city appears once, checking is instant.

Dict

{"pen": 20, "book": 150}

A price list: look up a product name and get its price instantly.

Brackets tell you the type: [ ] 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.

OrderedChangeableDuplicates OK

cart =

[0]
"pen"
[1]
"book"

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.

Find:·

List: checks one by one

DelhiMumbaiPuneGoaAgraSuratKochiIndore

Set: hash → jump to bucket

#0
Pune
#1
Indore
#2
#3
Mumbai
Agra
#4
#5
#6
Delhi
Kochi
#7
Goa
Surat
This is also why dict keys and set items must be unchangeable (str, int, tuple). A list as a key gives 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 looping
▼ loop
spam
▼ loop
spam
▼ loop
hi
▼ loop
spam

Loop 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:

customercityamount
AshaDelhi450
RaviPune1200
AshaDelhi300
orders = [
  {"customer": "Asha", "city": "Delhi", "amount": 450},
  {"customer": "Ravi", "city": "Pune",  "amount": 1200},
  {"customer": "Asha", "city": "Delhi", "amount": 300},
]   # csv.DictReader gives you exactly this

Open 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

Enjoying this free lesson? Your support helps us build more free animated chapters.

Support Us