Python Data Structures — Explain Like I'm 5
Different Containers for Different Jobs
Imagine your bedroom. You keep different things in different places:
- A bookshelf for books — ordered, you can find book #3 on the shelf
- A dictionary — you look up words by name, not by position
- A photo album — fixed set of photos in a fixed order, you can’t add more
- A jar of unique marbles — no duplicates, and you don’t care what order they’re in
Python has four main ways to store groups of things, and each one matches one of these ideas.
Lists: The Ordered Shelf
fruits = ["apple", "banana", "cherry"]
A list is like a numbered shelf. The first item is #0, the second is #1, and so on. You can add items, remove items, and change items. Order is preserved.
fruits[0] # "apple" — the first one
fruits.append("date") # Add to the end
fruits[1] = "blueberry" # Change the second one
Use a list when order matters and you need to change things.
Dictionaries: The Phonebook
phone_book = {
"Alice": "555-1234",
"Bob": "555-5678"
}
A dictionary doesn’t use position numbers. Instead, you look things up by a key (like a name):
phone_book["Alice"] # "555-1234"
You can add new entries, change existing ones, or remove them. Use a dictionary when you want to look things up by name, not by position.
Tuples: The Sealed Envelope
coordinates = (51.5, -0.12) # London's latitude and longitude
A tuple is like a list, but you can’t change it. Once you put things in, they stay. This makes it useful for things that shouldn’t change — like coordinates, RGB colors, or fixed settings.
Sets: The Jar of Unique Marbles
colors = {"red", "blue", "green"}
colors.add("red") # Still just {"red", "blue", "green"} — no duplicates!
A set automatically removes duplicates and doesn’t care about order. Useful when you want to check “is this thing in the group?” or combine groups without repeats.
Picking the Right One
| Situation | Use |
|---|---|
| Ordered list you’ll change | list |
| Look up by name/key | dict |
| Fixed, unchangeable data | tuple |
| Unique items, no order needed | set |
One Thing to Remember
Python’s four data structures — list, dict, tuple, set — each solve a different problem. Lists for ordered collections, dicts for lookup-by-name, tuples for fixed data, sets for unique items.
See Also
- Python Async Await Async/await helps one Python program juggle many waiting jobs at once, like a chef who keeps multiple pots moving without standing still.
- Python Basics Python is the programming language that reads like plain English — here's why millions of beginners (and experts) choose it first.
- Python Booleans Make Booleans click with one clear analogy you can reuse whenever Python feels confusing.
- Python Break Continue Make Break Continue click with one clear analogy you can reuse whenever Python feels confusing.
- Python Closures See how Python functions can remember private information, even after the outer function has already finished.