📖 Chapter 5: Lists & Dictionaries – Digital Bento Boxes of Doom

“Sometimes you don’t want just one variable. You want a bunch. Neatly stacked. Organized. Possibly with labels. Like sushi. Or trauma.”

🧃 Why We Need Data Structures

So far, you’ve been feeding your code little bites: one variable at a time.

snack1 = "apple"
snack2 = "chips"
snack3 = "sad granola bar"

Stop. You deserve better. We all do.

Meet lists and dictionaries: your way to bundle and organize all that data, so your functions don’t become hoarders with no filing system.


📦 Lists – Python’s Grocery Bag

A list is like a container. A box of things. It can hold:

snacks = ["apple", "chips", "sad granola bar"]

You can:

🧪 Cursor Try:

# Make a list of your top 5 favorite movies
# Print each one with its ranking

Let Cursor handle the loop logic. You handle the popcorn.


💡 Indexing 101: Computers Start Counting at 0

snacks = ["banana", "peanut", "tuna"]
print(snacks[0])  # banana

🧙 Fancy Looping

for snack in snacks:
    print("Delicious:", snack)

Or:

for i in range(len(snacks)):
    print("Item #" + str(i + 1) + ":", snacks[i])

Loops + lists = code that scales like a wizard elevator.


📚 Dictionaries – When Lists Just Aren’t Fancy Enough

Let’s say you want to store not just items, but meanings:

snack_info = {
    "apple": "healthy",
    "chips": "salty",
    "sad granola bar": "emotional support"
}

A dictionary (or dict) is a collection of key-value pairs.

Access like:

print(snack_info["chips"])  # Output: salty

Add/modify:

snack_info["cookie"] = "delightful mistake"

Loop:

for snack, vibe in snack_info.items():
    print(snack, "is", vibe)

🧠 Analogy Table

Data Type Real World Analogy
list A box full of unlabelled stuff
dict A library where each item has a label
Index A position in line
Key A name tag on a drawer

🧪 Cursor Prompt Time:

# Create a dictionary of planets and their number of moons
# Print each planet and say whether it’s "moon rich" or "moon poor"

Cursor will likely loop through your dictionary like a proud astrologer.

Add drama:


🎮 Mini Project: Build a Quiz Game

Use a dictionary for questions and answers.

# Ask Cursor to:
# - Create 3 questions and answers in a dictionary
# - Loop through them and ask the user
# - Track the score and give a final grade

Then:


✨ Bonus: Nested Data (The Lasagna of Code)

menu = {
    "breakfast": ["eggs", "pancakes"],
    "lunch": ["sandwich", "soup"],
    "dinner": ["sushi", "pizza"]
}

Access like:

print(menu["lunch"][1])  # soup

It’s turtles all the way down. But tasty turtles. 🐢


🤓 Recap Bento Box


🎁 Challenge: Build a Digital Organizer

# Create a to-do list app that uses a list of dictionaries
# Each task should have a name, priority, and status
# Add functions to display, add, and mark complete

Now you’re building software, not just code.


THE END of Chapter 5