Dictionaries - Meaning Over Position
Card 13 of 15

What Is a Dictionary?

"A collection of key-value pairs. Unlike lists where position matters, dictionaries use meaningful names (keys) to access values."

Lists vs Dictionaries

LIST

["John", 25, "Engineer"]
[0] → "John"
[1] → 25
[2] → "Engineer"
Access by position
Ordered collection
What does [1] mean?

DICTIONARY

{"name": "John", "age": 25, "job": "Engineer"}
["name"] → "John"
["age"] → 25
["job"] → "Engineer"
Access by key
Unordered bag
Self-documenting!
Key Difference: Lists use numbers (indices), dictionaries use meaningful names (keys). Dictionaries are self-documenting - the key tells you what the value means.

Interactive Dictionary Builder

Add key-value pairs to build dictionary

Dictionary Operations

# Creating a dictionary
person = {
    "name": "Alice",
    "age": 30,
    "job": "Engineer"
}

# Accessing values
name = person["name"]        # "Alice"
age = person["age"]          # 30

# Adding/updating values
person["city"] = "Boston"    # Add new key-value
person["age"] = 31           # Update existing

# Checking if key exists
has_job = "job" in person    # True

# Getting all keys/values
keys = person.keys()         # ["name", "age", "job", "city"]
values = person.values()     # ["Alice", 31, "Engineer", "Boston"]

When to Use Dictionaries

  • Named data: When values have meaningful labels
  • Lookup tables: Map one thing to another (word → definition)
  • Configuration: Settings with names, not positions
  • JSON-like data: Nested, structured information
"Dictionaries trade order for meaning. Position doesn't matter - the key's name tells you what the value represents. This makes code more readable and maintainable."

Real-World Example

# Student grades - dictionary makes sense
grades = {
    "Alice": 95,
    "Bob": 87,
    "Charlie": 92
}

# Look up by name (not position)
alice_grade = grades["Alice"]  # 95

# vs List approach (confusing!)
# grades = [95, 87, 92]
# alice_grade = grades[0]  # Who is index 0?
Mental Model: A dictionary is like a real dictionary or phone book. You look things up by name (key), not by page number (index). The name tells you what you're looking for.
[##########################....] 13/15
> [WHY_IT_MATTERS]:
Keys open doors. Dictionaries let you find the data without searching the whole world.
Cute Computer Mascot