Have you ever looked up a word in a dictionary? You find a key (the word) and its value (the meaning). Python’s dictionaries work the same way!
A dictionary is a collection of key-value pairs. It uses curly brackets { }. Each key is followed by a colon :, then the value. Pairs are separated by commas.
💡 Think of a contact list:
The name is the key
The phone number is the value
You use the name to find the number!
By the end of this lesson, you will be able to:
Create and change dictionaries in Python
Get values using keys
Use dictionary methods to update and organise data
Dictionaries – Store key-value pairs in { }
Keys – Unique names used to look up values
Values – The data saved with a key
Accessing items – Use dict.get(key)
Updating values – Use dict[key] = new_value
In Python, lists and dictionaries both store collections of data, but they work differently. A list stores items in a specific order, and you access each item by its position number (index) starting from 0. A dictionary, on the other hand, stores data as key-value pairs, and you access values using a unique key, not a number.
Lists are useful when the order of items matters, while dictionaries are better when you need to label data or quickly look up information by a name or ID. Both structures use different brackets: lists use [ ] and dictionaries use { }.
A dictionary stores key-value pairs. Each pair is made with a key, a colon :, and a value.
Example:
student = {"name": "Alice", "age": 14, "grade": "A"}
You can get values from the dictionary using the key:
print(student.get("age")) # Output: 14
Repeating keys:
{1: "one", 1: "uno"} # The second key will replace the first one
Asking for a missing key:
print(student.get("height")) # This gives: None
You can do more with dictionaries using methods.
.keys() – Get all the keys
print(student.keys())
# Output: dict_keys(['name', 'age', 'grade'])
.values() – Get all the values
print(student.values())
# Output: dict_values(['Alice', 14, 'A'])
.items() – Get all key-value pairs
print(student.items())
# Output: dict_items([('name', 'Alice'), ('age', 14), ('grade', 'A')])
.update() – Add or change a key-value pair
student.update({"grade": "B", "hobby": "painting"})
print(student)
# Output: {'name': 'Alice', 'age': 14, 'grade': 'B', 'hobby': 'painting'}
.pop(key) – Remove a key-value pair
student.pop("age")
print(student)
# Output: {'name': 'Alice', 'grade': 'B', 'hobby': 'painting'}
.clear() – Remove everything
student.clear()
print(student)
# Output: {}
🔗 Practice on W3Schools: Dictionaries Tutorial – Learn how to store and retrieve key-value pairs using dictionaries. There are additional methods that can make working with Dictionaries easier which you can practice here.
🔗 Try It Online: Run Your Code Here – Test your Python code instantly.
1️⃣ Basic Challenge:
Create a dictionary with your name, age, and favourite color as key-value pairs.
Print the dictionary.
2️⃣ Modify Challenge:
Modify your dictionary to add a new key-value pair for your favourite sport.
Print the updated dictionary.
3️⃣ Welcome Challenge:
Write a program that asks the user for their name, age, and hobby.
Store this data in a dictionary and print a formatted sentence like:
"Hi, my name is ___. I'm ___ years old and I like ___."
4️⃣ Favourite Foods Challenge:
Create a dictionary of 3 friends and their favourite foods.
Print all the keys using .keys()
Print all the values using .values()
5️⃣ Update My Info Challenge:
Start with this dictionary:
me = {"name": "Leo", "age": 13}
Add a "hobby" using .update()
Change "age" to 14
Print the updated dictionary
6️⃣ Clean-Up Time Challenge:
Make a dictionary with 4 key-value pairs (e.g. pet info).
Remove one key with .pop()
Clear the whole dictionary with .clear()
Print after each step
7️⃣ Safe Search Challenge:
Use this dictionary:
subject = {"name": "Math", "teacher": "Mr. Hill", "room": "A1"}
Try print(subject["name"]) and print(subject["time"])
Then try print(subject.get("time"))
Observe and explain the difference
8️⃣ Show Everything Challenge:
Create a robot dictionary (e.g. {"model": "XR7", "task": "cleaning", "battery": "80%"})
Use .items() to print all key-value pairs neatly
Write the example from this lesson, ensuring you include:
✅ A dictionary storing key-value pairs
✅ Accessing values using dictionary keys
✅ A print() statement displaying values from the dictionary
Read the example code before starting!