Module 5: Lists & Dictionaries (Organizing Your Data)

Welcome to Module 5! Up until now, we’ve been using one variable for one piece of data (e.g., name = "Alex"). But what if you are building a game and need to store the names of 100 players? Or a shopping app with 500 items?

You don’t want to create 500 variables! Instead, we use Data Structures to keep our information organized.


1. Lists (The Shopping List)

A List is an ordered collection of items. You can put strings, numbers, or even other lists inside them.

The Code:

Python

fruits = ["Apple", "Banana", "Cherry"]

# Accessing items: Python starts counting at 0!
print(fruits[0])  # Output: Apple
print(fruits[1])  # Output: Banana

# Adding an item
fruits.append("Orange")

# Changing an item
fruits[0] = "Blueberry"

2. Dictionaries (The Digital Filing Cabinet)

While Lists are great for ordered items, Dictionaries are best for connecting two pieces of information together using “Key-Value Pairs.”

Think of a real dictionary: you look up a Word (Key) to find its Definition (Value).

The Code:

Python

# Creating a dictionary
user = {
    "name": "Alex",
    "level": 10,
    "is_pro": True
}

# Accessing data by using the Key
print(user["name"])  # Output: Alex

# Adding or updating a key
user["level"] = 11
user["email"] = "[email protected]"

3. Combining Loops and Lists

The most common task in programming is “looping through a list.” This allows you to perform an action on every single item in a collection.

The Code:

Python

guests = ["Alice", "Bob", "Charlie", "Diana"]

for guest in guests:
    print("Welcome to the party, " + guest + "!")

🎓 Module 5 Final Project: “The Simple Todo List” 📝

We are going to build a program that lets a user build a list of tasks.

The Challenge:

  1. Create an empty list called todo_list.
  2. Use a while loop to keep asking the user for a task.
  3. If they type “show”, print the entire list.
  4. If they type “quit”, stop the program.
  5. Otherwise, append their input to the list.

The Solution:

This brings together Lists, Loops, and Conditionals!

Python

todo_list = []
active = True

while active: task = input(“Enter a task (or type ‘show’ or ‘quit’): “)

if task == "quit":
    active = False
elif task == "show":
    print("Your tasks: " + str(todo_list))
else:
    todo_list.append(task)
    print("Task added!")

Next Up: The Final Capstone Project! In the next module, we will combine everything from Modules 1-5 to build a complete, functional application.