Lesson 12 · Data Modeling

Everything Connected to Everything: Mapping Relationships Like a Detective

What You'll Learn

How to model real-world relationships using dictionaries — Python's tool for connecting names to values — and build a game that tracks choices and preferences.

The Problem With Lists

Lists are great for ordered collections — a playlist, a to-do list, a series of scores. But what happens when you need to look something up by name?

Imagine storing info about a game character:

Python · The Awkward Way

Remembering that health is at position 0, attack at position 1... that's fragile and confusing. What we really want is to look things up by name: "give me the health value." Python has the perfect tool for this.

Dictionaries: Names → Values

A dictionary connects keys (names) to values. Instead of looking things up by position number, you look them up by name:

Python · Dictionaries

How Dictionaries Work

Curly braces {} create a dictionary. Inside: "key": value pairs, separated by commas.

character["health"] looks up the value connected to the key "health" — gives us 100.

Unlike lists, there are no position numbers. You access things by name. "health" could be the first item or the last — it doesn't matter.

Think of it like a real dictionary: you look up the word (key) and find its definition (value).

Adding, Changing, and Removing

Python

Dictionary Operations

character["health"] = 80 — change an existing value

character["magic"] = 50 — add a new key-value pair (if the key doesn't exist, it's created)

del character["attack"] — remove a key and its value

"magic" in character — check if a key exists (True/False)

Looping Through a Dictionary

Python

Looping Options

for key in character: — loops through just the keys

for key, value in character.items(): — loops through both keys and values at the same time

character.keys() — all the keys as a list

character.values() — all the values as a list

Nesting Dictionaries

Here's where dictionaries get really powerful: a value inside a dictionary can be another dictionary. This lets you model complex, real-world relationships:

Python · Nested Dictionaries

Reading Nested Dictionaries

friends["Lena"] gives us Lena's whole dictionary: {"age": 11, "hobby": "soccer", "level": 9}

friends["Lena"]["hobby"] goes one level deeper: "soccer"

Think of it as drilling down: "In the friends dictionary, find Lena. In Lena's dictionary, find hobby." Each pair of square brackets goes one level deeper.

Modeling the Game Data

Now let's use dictionaries to build a "Would You Rather?" game. First, we need to model the data — each question has two options, a category, and a difficulty:

Python · Game Data

Lists of Dictionaries

questions is a list where each item is a dictionary. This is a very common pattern — it's how most real data is organized.

enumerate(questions) gives both the index number and the item, so we can number them.

Each question has the same structure (option_a, option_b, category, difficulty) but different content. That's data modeling — choosing what properties matter and keeping them consistent.

Tracking Choices

A game is more fun when it tracks what people pick. Let's use a dictionary to count votes:

Python · Vote Tracking

The Complete "Would You Rather?" Game

Python · Complete Game

The Art of Data Modeling

What you just did — deciding how to structure the question data, the vote tracking, the player stats — is called data modeling. It's one of the most important skills in programming.

Data modeling means asking: "What are the things in my problem? What properties do they have? How do they connect?"

Data Modeling Decisions We Made

A question has: two options, a category, a difficulty → dictionary with 4 keys

A collection of questions → list of dictionaries

Player stats has: total answers, A-count, B-count, categories → nested dictionary

Vote results for a question → dictionary {"A": count, "B": count}

Different problems need different models. A contact list might be a dictionary of names → phone numbers. A game inventory might be a dictionary of item names → quantities. A social network might be a dictionary of people → lists of their friends.

Python · Different Models

Each model captures different relationships. Contacts connect names to numbers. Inventory connects items to quantities. A social network connects people to lists of other people. The dictionary is flexible enough to model all of them.

What You Just Did

You learned to model real-world relationships using dictionaries — connecting names to values, objects to properties, and ideas to details. You built a game where data is structured, tracked, and analyzed.

That's data modeling.

Every app you use relies on data modeling. Instagram models posts (image + caption + likes + comments). Spotify models songs (title + artist + duration + album). Games model characters (stats + inventory + position). The model determines what the app can do — because you can only work with data you've structured.

The Big Idea

Before you write code, ask: "What are the things in my problem? What properties do they have? How are they connected?" Choose the right structure — lists for ordered collections, dictionaries for named lookups, nested combinations for complex relationships — and the code almost writes itself.

Thinking Questions

  1. When would you use a list vs. a dictionary? What's the key difference in how you access items?
  2. If you were building a library system, what would you model? What would the dictionaries look like?
  3. Why is choosing the right data model important before you start coding?

Level Up

Challenge 1 · Pokédex

Create a dictionary of at least 5 creatures, each with name, type, health, and attack power. Let the user search by name and display the creature's stats. Add a "battle" feature that compares two creatures' attack power.

Challenge 2 · Multi-Player Voting

Expand the "Would You Rather?" game to support multiple players. Track each player's choices in a dictionary: {"Lena": ["A", "B", "A"], "Sam": ["B", "B", "A"]}. At the end, show where players agreed and disagreed.

Challenge 3 · Inventory System

Build a game inventory where the player can: view items, add items, use items (decrease count), and drop items (remove entirely). Use a dictionary to store item names and counts. Add item categories (weapon, food, tool) using nested dictionaries.

What You Learned

Thinking Skill

Data Modeling — representing real-world things and relationships in code by asking: "What are the essential properties, and how do they connect?"

Python Concepts

Dictionaries{"key": value} for named lookups

Accessdict["key"] to get a value

Modifydict["key"] = new_value

Adddict["new_key"] = value

Deletedel dict["key"]

Check existence"key" in dict

Loopfor key, value in dict.items():

Nested dictionaries — dictionaries inside dictionaries

Lists of dictionaries — a common pattern for structured data

enumerate() — loop with both index and item

.get(key, default) — safe lookup with a fallback value