Lesson 4 of 6

Lists, Dicts & Data Structures

A single variable holds one thing. Real programs need to hold collections of things — a shopping list, a set of user profiles, a row from a spreadsheet. Python gives you a small set of built-in containers for exactly this, and picking the right one is one of the first real design decisions you'll make in any script.

Lists: an ordered, changeable sequence

A list is what most languages call an array — an ordered collection you can add to, remove from, and index into by position, starting at 0:

fruits = ["apple", "banana", "cherry"]
print(fruits[0])          # apple
fruits.append("date")     # add to the end
fruits[1] = "blueberry"   # replace by position
print(fruits)             # ['apple', 'blueberry', 'cherry', 'date']

Lists are your default choice whenever order matters and the collection might grow or shrink — a queue of tasks, a list of scores, the lines read from a file.

💡 The Bottom Line

Use a list when you have an ordered, changeable sequence of items. Use a dict when you need to look things up by a meaningful name instead of a position. Use a tuple when the collection should never change once created. Picking the right one up front saves you from fighting the wrong tool later.

Dictionaries: lookup by name, not position

A dict (dictionary) stores key → value pairs. Instead of "give me item number 2," you ask "give me the value stored under this key":

person = {"name": "Ada", "age": 29, "city": "London"}
print(person["name"])      # Ada
person["age"] = 30         # update by key
person["job"] = "Engineer" # add a new key
print(person)

Dicts are the natural fit whenever your data has named fields — a user record, a config file, a single row pulled from an API response. If you find yourself keeping two parallel lists in sync (one of names, one of ages, matched by position), that's usually a sign you actually wanted a dict or a list of dicts.

🙋 There Are No Dumb Questions

Q: What's a tuple, and why not just use a list for everything?
A: A tuple looks like a list but uses parentheses, (1, 2, 3), and once created it can't be changed — no appending, no reassigning an item. That's a feature, not a limitation: it signals "this data is fixed" (like a pair of coordinates, or a date made of (year, month, day)), and Python will stop you if you or a teammate accidentally try to modify it later.

Indexing and slicing

Both lists and strings support slicing — pulling out a sub-range using [start:stop], where stop is not included:

numbers = [10, 20, 30, 40, 50]
print(numbers[1:3])   # [20, 30]
print(numbers[:2])    # [10, 20]   -- from the start
print(numbers[3:])    # [40, 50]   -- to the end
print(numbers[-1])    # 50         -- negative index counts from the end

That negative-index trick — -1 for "the last item," -2 for "second to last" — is used everywhere in real Python code, so it's worth committing to memory early.

⚠️ Watch Out

Dict keys must be unique — assigning to an existing key overwrites it rather than adding a second entry. And trying to access a key that doesn't exist raises a KeyError rather than quietly returning nothing. Use person.get("nickname") instead of person["nickname"] when a key might be missing — .get() returns None instead of crashing.

🔧 Try It Yourself

Build a small dictionary representing a book, with keys title, author, and pages. Then build a list called shelf containing three such book dictionaries. Finally, print just the title of the second book on the shelf:

book1 = {"title": "Dune", "author": "Frank Herbert", "pages": 412}
book2 = {"title": "1984", "author": "George Orwell", "pages": 328}
book3 = {"title": "Emma", "author": "Jane Austen", "pages": 474}

shelf = [book1, book2, book3]
print(shelf[1]["title"])   # 1984

Notice how the list gives you position (shelf[1]) and the dict inside it gives you a named field (["title"]) — combining containers like this is how real Python data is usually shaped.

✅ Quick Check

You need to store a person's name, age, and email, and look each one up by its label. Which structure fits best?


Next up: now that you can store real data, it's time to organize the code that acts on it. Lesson 5 covers functions — how to package logic so you write it once and reuse it everywhere.

← Back to
Next →