13 Organizing Information: Lists and Dictionaries
13.1 The Concept First
So far, we’ve stored one piece of information per variable. But real-world programs need to handle collections: class rosters, shopping lists, contact books, inventory systems.
Imagine trying to manage a party guest list where each guest needs their own variable: guest1, guest2, guest3… guest50. Chaos!
Lists and dictionaries are programming’s organizational tools - like having folders and filing cabinets instead of scattered papers.
13.2 Understanding Through Real Life
Lists: Ordered Collections
Think about everyday lists: - Shopping list: milk, bread, eggs, cheese (order might matter for store layout) - To-do list: homework, chores, practice, sleep (order definitely matters!) - Playlist: songs in the sequence you want to hear them - Class roster: students listed alphabetically
Lists keep items in order and let you work with the whole collection.
Dictionaries: Labeled Storage
Think about labeled organization: - Phone contacts: “Mom” → 555-1234, “Pizza Place” → 555-5678 - Student grades: “Alice” → 95, “Bob” → 87, “Charlie” → 92 - Game inventory: “health potions” → 3, “gold” → 150, “arrows” → 25 - Recipe ingredients: “flour” → “2 cups”, “sugar” → “1 cup”
Dictionaries connect labels (keys) to values, making information easy to find.
Why We Need Both
- Lists answer: “What’s the 3rd item?” or “Give me everything in order”
- Dictionaries answer: “What’s Alice’s phone number?” or “How much gold do I have?”
Different organizational needs require different tools.
13.3 Discovering Collections with Your AI Partner
Let’s explore how programs organize multiple pieces of information.
Exploration 1: The Problem with Many Variables
Ask your AI:
Show me code that stores 10 student names using individual variables.
Then show me the same thing using a list.
Notice how lists eliminate variable explosion?
Exploration 2: Finding vs Position
Try this prompt:
Compare finding "Sarah's phone number" in a list vs a dictionary.
Show me why dictionaries are better for lookup.
This reveals when to use each type.
Exploration 3: Real Program Needs
Ask:
What kind of information would a simple game need to track?
Show examples using both lists and dictionaries.
You’ll see how real programs combine both types.
13.4 From Concept to Code
Let’s see how Python implements these organizational tools.
Lists: Your First Collection
Ask your AI:
Show me the simplest possible Python list with 3 fruits,
and how to display them all.
You’ll get something like:
fruits = ["apple", "banana", "orange"]
print(fruits) # ['apple', 'banana', 'orange']
# Access individual items by position (starting at 0!)
print(fruits[0]) # apple
print(fruits[1]) # banana
print(fruits[2]) # orangeDictionaries: Labeled Information
Now for dictionaries:
phone_book = {
"Mom": "555-1234",
"Pizza": "555-5678",
"School": "555-9999"
}
# Look up by label
print(phone_book["Mom"]) # 555-1234Labels make finding information natural!
13.5 Mental Model Building
Model 1: Lists as Trains
[car0] → [car1] → [car2] → [car3]
"apple" "banana" "orange" "grape"
Access by position: fruits[2] gets third car
Model 2: Dictionaries as Filing Cabinets
┌─────────────┐
│ Mom │ → "555-1234"
├─────────────┤
│ Pizza │ → "555-5678"
├─────────────┤
│ School │ → "555-9999"
└─────────────┘
Access by label: phone_book["Pizza"]
Model 3: Lists vs Dictionaries
List: "What's in position 3?"
[0][1][2][3][4]
↑ ↑ ↑ ↑ ↑
🍎 🍌 🍊 🍇 🍓
Dictionary: "What's the capital of France?"
{"France": "Paris",
"Spain": "Madrid",
"Italy": "Rome"}
13.6 Prompt Evolution Exercise
Let’s practice getting collection examples from AI.
Round 1: Too Vague
show me lists and dictionaries
You’ll get advanced features like comprehensions, nested structures, and methods galore!
Round 2: More Specific
show me Python lists and dictionaries for beginners
Better, but might still be overwhelming.
Round 3: Learning-Focused
I'm learning to organize multiple pieces of information. Show me a simple
shopping list using a Python list, and a simple contact book using a dictionary.
Perfect for understanding!
Round 4: Practical Application
Now show me how to add items to the shopping list and look up a contact
This shows basic operations.
13.7 Common AI Complications
When you ask AI about lists and dictionaries, it often gives you:
class InventoryManager:
def __init__(self):
self.inventory = defaultdict(lambda: {'quantity': 0, 'price': 0.0})
self.categories = defaultdict(list)
self.low_stock_threshold = 10
def add_item(self, item_id, name, quantity, price, category):
self.inventory[item_id] = {
'name': name,
'quantity': quantity,
'price': price,
'category': category,
'last_updated': datetime.now()
}
self.categories[category].append(item_id)
def update_quantity(self, item_id, quantity_change):
if item_id in self.inventory:
self.inventory[item_id]['quantity'] += quantity_change
if self.inventory[item_id]['quantity'] < self.low_stock_threshold:
self._trigger_reorder(item_id)
def get_category_value(self, category):
return sum(self.inventory[item_id]['quantity'] *
self.inventory[item_id]['price']
for item_id in self.categories[category])Classes! Default dictionaries! Nested structures! Datetime! This is enterprise inventory management, not learning collections!
13.8 The Learning Approach
Build understanding step by step:
Level 1: Simple Lists
# Creating and using lists
colors = ["red", "blue", "green"]
print(colors) # See all
print(colors[0]) # First item
print(len(colors)) # How many
# Lists can change!
colors.append("yellow")
print(colors) # Now has 4 itemsLevel 2: Simple Dictionaries
# Creating and using dictionaries
scores = {
"Alice": 95,
"Bob": 87,
"Charlie": 92
}
print(scores["Alice"]) # Get Alice's score
scores["David"] = 88 # Add new student
print(scores) # See all scoresLevel 3: Lists in Loops
# Process each item
shopping = ["milk", "eggs", "bread"]
print("Shopping list:")
for item in shopping:
print(f"- {item}")
# Add user items
new_item = input("Add item: ")
shopping.append(new_item)Level 4: Practical Combinations
# Real programs use both!
# List of dictionaries
students = [
{"name": "Alice", "grade": 95},
{"name": "Bob", "grade": 87},
{"name": "Charlie", "grade": 92}
]
# Process all students
for student in students:
print(f"{student['name']}: {student['grade']}")Lists use [] with numbers (indices) starting at 0: - fruits[0] - First item - fruits[1] - Second item - fruits[-1] - Last item (negative counts from end!) - len(fruits) - How many items
Ask AI: “Why do programming lists start at 0 instead of 1?”
13.9 Exercises
Exercise 7.1: Concept Recognition
Identifying Collection Needs
For each scenario, decide: list or dictionary?
- Track player scores in order from highest to lowest
- Store student ID numbers and their corresponding names
- Keep a sequence of moves in a game
- Store item prices in a store
- Remember the order of people in a queue
Check Your Answers
- List - order matters, positions important
- Dictionary - need to look up name by ID
- List - sequence/order is critical
- Dictionary - look up price by item name
- List - order determines who’s next
Exercise 7.2: Prompt Engineering
Getting Collection Examples
Start with: “grade tracker”
Evolve this prompt to get AI to show you: 1. A dictionary storing student names and grades 2. How to add a new student 3. How to update a grade 4. How to calculate class average
Document your prompt evolution.
Effective Final Prompt
“Show me a simple Python program that: 1. Uses a dictionary to store student names and their grades 2. Shows how to add a new student 3. Shows how to update an existing grade 4. Calculates the class average Keep it beginner-friendly with no classes or advanced features”Exercise 7.3: Pattern Matching
Simplifying Complex Collections
Ask AI for a “professional task management system”. In the complex code: 1. Find all lists and dictionaries 2. Identify what each stores 3. Rewrite using just 1-2 simple collections
Core Collections Needed
You probably just need: - tasks - list of task names or dictionaries - Maybe priorities - dictionary of task: priority
Exercise 7.4: Build a Model
Visualizing Collections
Create visual models for: 1. A list of your 5 favorite songs showing index positions 2. A dictionary of 4 friends and their birthdays 3. A diagram showing when to use list vs dictionary
Make your models clear enough to teach someone else.
Exercise 7.5: Architect First
Design Collection-Based Programs
Design these programs before coding:
- Class Roster System
- Store student names (list or dict?)
- Track attendance for each
- Calculate attendance percentage
- Simple Menu System
- Food items with prices
- Customer order list
- Calculate total bill
- Game Inventory
- Items player has
- Quantity of each
- Add/remove items
For each, specify: - What collections you need - What type (list/dictionary) and why - How they work together
Then ask AI: “Implement this design: [your specification]”
Design Example
Menu System Design: -menu - dictionary {“Pizza”: 12.99, “Burger”: 8.99, …} - order - list of items ordered [“Pizza”, “Burger”, “Pizza”] - Process: Loop through order, look up each price in menu, sum total
13.10 AI Partnership Patterns
Pattern 1: Collection Conversion
Show AI different organizations: - “Convert these 10 variables into a list” - “Convert this numbered list into a dictionary” - “Show me why one is better than the other here”
Pattern 2: Building Operations
Learn operations gradually: 1. “Create a simple list of colors” 2. “Add a new color to the list” 3. “Remove ‘blue’ from the list” 4. “Check if ‘red’ is in the list”
Pattern 3: Real-World Modeling
Connect to familiar systems: - “Model a playlist using a list” - “Model a phonebook using a dictionary” - “Model a shopping cart using both”
13.11 Common Misconceptions
“Lists and arrays are the same”
Reality: In Python, lists are flexible:
mixed_list = ["apple", 42, True, 3.14] # Different types OK!“Dictionaries maintain order”
Reality: Modern Python keeps insertion order, but dictionaries are designed for lookup, not sequence:
# Use dict for lookup
phone_book = {"Alice": "555-1234"}
# Use list for sequence
playlist = ["Song 1", "Song 2", "Song 3"]“You can only store simple values”
Reality: Collections can store anything, even other collections:
# List of lists
grid = [
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
]
# Dictionary of lists
student_grades = {
"Alice": [95, 87, 92],
"Bob": [88, 91, 86]
}13.12 Real-World Connection
Every app uses collections:
Music App:
playlist = ["Song A", "Song B", "Song C"]
song_info = {
"Song A": {"artist": "Artist 1", "duration": 180},
"Song B": {"artist": "Artist 2", "duration": 210}
}E-commerce:
shopping_cart = ["laptop", "mouse", "keyboard"]
prices = {
"laptop": 999.99,
"mouse": 29.99,
"keyboard": 79.99
}Social Media:
friends = ["Alice", "Bob", "Charlie"]
profiles = {
"Alice": {"status": "Online", "posts": 45},
"Bob": {"status": "Away", "posts": 132}
}13.13 Chapter Summary
You’ve learned: - Lists store ordered collections accessible by position - Dictionaries store labeled information accessible by key - Both can be modified after creation - Lists excel at sequences, dictionaries at lookups - Real programs often use both together
13.14 Reflection Checklist
Before moving to Chapter 8, ensure you:
13.15 Your Learning Journal
For this chapter, record:
- Collection Mapping: List 5 real-world lists and 5 real-world dictionaries
- Code Comparison: Solve the same problem with/without collections
- Mental Models: Draw your visualization of lists and dictionaries
- Design Practice: Plan collections for a simple library system
- Use a list when: Order matters, you need positions, you’ll process all items
- Use a dictionary when: You need labels, you’ll look things up, order doesn’t matter
- Use both when: You have complex data (list of student dictionaries)
13.16 Next Steps
In Chapter 8, we’ll learn how to save and load data from files. You’ll see how collections become even more powerful when you can preserve them between program runs - turning temporary programs into persistent applications!
Remember: Lists and dictionaries aren’t about syntax. They’re about choosing the right organizational tool for your data - just like choosing between a numbered list or a labeled filing system in real life!