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?
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.
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 elementcodemon_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 keyWith 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?
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:
Exercise 8 — Create a Codémon Team Dictionary
Complete the code to create a dictionary storing information about your Codémon team leader.
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?
"name" and "level""Pyrochu" and 25"name": "Pyrochu" and "level": 25codemonHint : 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
# Create your Codémon dictionary here
# Print the dictionary
# Print just the abilities