Python Dictionaries: Map Keys to Values
Dictionaries let you store structured data with named keys instead of numeric positions. Learn how to create, access, and modify dicts—the foundation of real-world data handling.
Week 1 we used tuples to store a sale: ('Alice Chen', 1250.00). Then you said the real CSV has four fields: name, amount, region, status. When I write sale[2], what is 2? Is it region or status? I have to remember.
Exactly. That's the pain point. You're treating data like it's a numbered row in a spreadsheet. But when you read your data six months from now—or when someone else reads your code—the numbers are meaningless. sale[0] could mean anything.
Today's the day you fix that. A dictionary lets you replace those numbers with names.
So instead of sale[0] for the name, I can say sale['name']?
Yes. A dictionary is a collection of key-value pairs. You create it with curly braces, and you access it with the key you chose. Here's the pattern:
# Tuple: mystery numbers
sale_tuple = ('Alice Chen', 1250.00, 'West', 'confirmed')
print(sale_tuple[0]) # Alice Chen — but what does 0 mean?
# Dictionary: labeled keys
sale_dict = {
'name': 'Alice Chen',
'amount': 1250.00,
'region': 'West',
'status': 'confirmed'
}
print(sale_dict['name']) # Alice Chen — clear intent
print(sale_dict['region']) # West — no ambiguity
print(sale_dict['amount']) # 1250.0
Instead of numbering, you name the slots. Sam always uses a filing cabinet analogy: a tuple is like a pile of papers numbered 1, 2, 3, 4. A dictionary is a filing cabinet with labeled drawers. You don't pull drawer #2; you pull the drawer labeled 'amount'.
So when I write that function to build a sale record from the CSV, I can return a dictionary with those four keys?
That's the whole point. Watch—you're going to write a function called make_sale_record that takes four parameters (name, amount, region, status) and returns a dictionary:
def make_sale_record(name, amount, region, status):
return {
'name': name,
'amount': amount,
'region': region,
'status': status
}
record = make_sale_record('Alice Chen', 1250.00, 'West', 'confirmed')
print(record['name']) # Alice Chen
print(record['amount']) # 1250.0
print(record['region']) # West
Now your code reads like a business problem, not a math puzzle.
I notice the keys are strings—single quotes around 'name'. Do they have to be strings?
In practice, yes. They can technically be other types, but strings are the standard. Think of them as labels. And the values can be any type: strings, numbers, booleans, even other lists and dictionaries. A real business record might also have a 'created_at' timestamp or a 'payment_method' list.
So a dictionary is basically the data structure for the real world. Every row in a CSV is a dictionary where the column headers are the keys.
Exactly. When you graduate from "processing one sale at a time" to "building a list of sales that you can filter, sort, and analyze," you need this. A list of dictionaries is how Python programs store structured data. It's how every API returns data. It's what you serialize to JSON and send to the frontend.
And tomorrow we learn what to do when a key doesn't exist, right? Like, what if a dictionary doesn't have a 'status' field?
That's exactly tomorrow's problem—safe key access with .get(). Today you're building records where you control the keys. Next lesson, you handle dictionaries where keys might be missing. One step at a time.
Practice your skills
Sign up to write and run code in this lesson.
Python Dictionaries: Map Keys to Values
Dictionaries let you store structured data with named keys instead of numeric positions. Learn how to create, access, and modify dicts—the foundation of real-world data handling.
Week 1 we used tuples to store a sale: ('Alice Chen', 1250.00). Then you said the real CSV has four fields: name, amount, region, status. When I write sale[2], what is 2? Is it region or status? I have to remember.
Exactly. That's the pain point. You're treating data like it's a numbered row in a spreadsheet. But when you read your data six months from now—or when someone else reads your code—the numbers are meaningless. sale[0] could mean anything.
Today's the day you fix that. A dictionary lets you replace those numbers with names.
So instead of sale[0] for the name, I can say sale['name']?
Yes. A dictionary is a collection of key-value pairs. You create it with curly braces, and you access it with the key you chose. Here's the pattern:
# Tuple: mystery numbers
sale_tuple = ('Alice Chen', 1250.00, 'West', 'confirmed')
print(sale_tuple[0]) # Alice Chen — but what does 0 mean?
# Dictionary: labeled keys
sale_dict = {
'name': 'Alice Chen',
'amount': 1250.00,
'region': 'West',
'status': 'confirmed'
}
print(sale_dict['name']) # Alice Chen — clear intent
print(sale_dict['region']) # West — no ambiguity
print(sale_dict['amount']) # 1250.0
Instead of numbering, you name the slots. Sam always uses a filing cabinet analogy: a tuple is like a pile of papers numbered 1, 2, 3, 4. A dictionary is a filing cabinet with labeled drawers. You don't pull drawer #2; you pull the drawer labeled 'amount'.
So when I write that function to build a sale record from the CSV, I can return a dictionary with those four keys?
That's the whole point. Watch—you're going to write a function called make_sale_record that takes four parameters (name, amount, region, status) and returns a dictionary:
def make_sale_record(name, amount, region, status):
return {
'name': name,
'amount': amount,
'region': region,
'status': status
}
record = make_sale_record('Alice Chen', 1250.00, 'West', 'confirmed')
print(record['name']) # Alice Chen
print(record['amount']) # 1250.0
print(record['region']) # West
Now your code reads like a business problem, not a math puzzle.
I notice the keys are strings—single quotes around 'name'. Do they have to be strings?
In practice, yes. They can technically be other types, but strings are the standard. Think of them as labels. And the values can be any type: strings, numbers, booleans, even other lists and dictionaries. A real business record might also have a 'created_at' timestamp or a 'payment_method' list.
So a dictionary is basically the data structure for the real world. Every row in a CSV is a dictionary where the column headers are the keys.
Exactly. When you graduate from "processing one sale at a time" to "building a list of sales that you can filter, sort, and analyze," you need this. A list of dictionaries is how Python programs store structured data. It's how every API returns data. It's what you serialize to JSON and send to the frontend.
And tomorrow we learn what to do when a key doesn't exist, right? Like, what if a dictionary doesn't have a 'status' field?
That's exactly tomorrow's problem—safe key access with .get(). Today you're building records where you control the keys. Next lesson, you handle dictionaries where keys might be missing. One step at a time.