Python · Dictionaries
Intro to Dictionaries
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercisesExercise 1 — What is a Dictionary?
A dictionary in Python is a collection of key-value pairs. Think of it like a real dictionary where you look up a word (the key) to find its definition (the value).
Dictionaries are created using curly braces
{}
with keys and values separated by colons
:
codemon = {
"name": "Aquachu",
"type": "water",
"level": 15
}
print(codemon)
print(type(codemon))
In this example:
-
"name"
,
"type"
, and
"level"
are the keys
-
"Aquachu"
,
"water"
, and
15
are the values
- Each key is associated with exactly one value
Exercise 2 — Dictionary Syntax and Structure
The basic syntax for creating a dictionary is:
dictionary_name = {key1: value1, key2: value2, key3: value3}
You can also create an empty dictionary and add elements later.
empty_dict = {}
print("Empty dictionary:", empty_dict)
Dictionary Values Can Be Anything
Dictionary values can be of any type: strings, numbers, booleans, lists, or even other dictionaries! Keys, however, must be immutable (like strings or numbers).
codemon = {
"name": "Pyrochu",
"type": "fire",
"level": 20,
"is_evolved": False
}
print("\nCodemon dictionary:", codemon)
Exercise 3 — Valid Dictionary Syntax
Which of the following is the correct way to create a dictionary in Python?
Options
• ```codemon = {"name": "Florachu", "type": "plant"}```
• ```codemon = ["name": "Florachu", "type": "plant"]```
• ```codemon = {"name" = "Florachu", "type" = "plant"}```
• ```codemon = ("name": "Florachu", "type": "plant")```
Hint : Remember: dictionaries use curly braces {} and colons : to separate keys from values.
Exercise 4 — Create Your First Dictionary
Complete the code to create a dictionary for a Codémon with its name and power level.
codemon = ____"name"____ "Aquachu", "power"____ 75____
print(codemon)
Options: { : : } [ ] = ,
Hint : Dictionaries are created with curly braces, and each key-value pair is separated by a colon.
Exercise 5 — Dictionaries vs Lists
What's the main difference between dictionaries and lists?
Lists use numeric indices (0, 1, 2...) to access elements, while dictionaries use custom keys (strings, numbers, etc.).
codemon_list = ["Aquachu", "water", 15]
# List: access by position (index)
print("List access by index:")
print(codemon_list[0]) # First element
print(codemon_list[1]) # Second element
codemon_dict = {
"name": "Aquachu",
"type": "water",
"level": 15
}
# Dictionary: access by key name
print("\nDictionary access by key:")
print(codemon_dict["name"]) # Access by key
print(codemon_dict["type"]) # Access by key
With dictionaries, the order doesn't matter as much because you access values by their meaningful key names, not by remembering their position.
Exercise 6 — When to Use Dictionaries
Which scenario is BEST suited for using a dictionary instead of a list?
Options
• Storing a Codémon's attributes like name, type, level, and power with labeled access
• Storing a sequence of high scores in order from highest to lowest
• Keeping a list of player names in the order they joined the game
• Storing coordinates as (x, y) pairs in sequence
Hint : Think about when meaningful labels (keys) are more useful than numeric positions (indices).
Exercise 7 — Dictionary Use Cases
Match each scenario with whether it's better suited for a dictionary or a list:
Items to match
• Store a Codémon's name, type, and stats
• Track top 10 scores in order
• Store a Team of Codémon with their types, levels, and abilities
• Keep items in a shopping cart
Definitions
• Dictionary (labeled data)
• List (ordered sequence)
• Array of dictionaries
• List (ordered sequence)
Exercise 8 — Create a Codémon Team Dictionary
Complete the code to create a dictionary storing information about your Codémon team leader.
team_leader = ____
"trainer_name"____ "Pierre",
"team_size"____ 6,
"badges"____ 8
____
print(team_leader)
Options: { : : : } [ ] =
Hint : Remember the dictionary structure: curly braces for the container, colons to separate keys from values.
Exercise 9 — Understanding Keys and Values
Given the dictionary:
codemon = {"name": "Pyrochu", "level": 25}
What are the keys in this dictionary?
Options
• ```"name"``` and ```"level"```
• ```"Pyrochu"``` and ```25```
• ```"name": "Pyrochu"``` and ```"level": 25```
• ```codemon```
Hint : Remember: keys are on the left side of the colon, values are on the right side.
Exercise 10 — Challenge: Create a Complete Codémon
Create a complete Codémon dictionary with the following information:
1. Create a dictionary named
codemon
with:
- A key
"name"
with the value
"Florachu"
- A key
"level"
with the value
30
- A key
"abilities"
with a list containing:
"Solar Beam"
,
"Vine Whip"
, and
"Synthesis"
2. Print the dictionary and the abilities
Starter code
# Create your Codémon dictionary here
# Print the dictionary
# Print just the abilities
Back to the course