Dictionaries
Store data with key-value pairs
Dictionaries are a super handy way to store information that needs to be organized and quickly found, like an actual dictionary or a phone book.
What are dictionaries?
Imagine you have a group of friends and you want to store their names and ages. You could use two lists, but how would you know which age belongs to which friend?
This is where dictionaries shine! They let you store data in pairs: a "key" that describes the data, and a "value" which is the data itself. Each key is unique, just like a word in a dictionary.
Creating dictionaries
You create a dictionary using curly braces . Inside, you list your key-value pairs, separated by colons :. Each pair is separated from the next by a comma ,.
my_friend = {
"name": "Alice",
"age": 30,
"city": "New York"
}
print(my_friend)Keys are usually strings, but they can be other immutable types like numbers or tuples. Values can be anything!
Accessing values
To get a specific piece of information, you use the key. It's like looking up a word in a dictionary. You put the key in square brackets [] after the dictionary's name.
my_friend = {"name": "Bob", "age": 25}
print(my_friend["name"])
print(my_friend["age"])Changing dictionaries
You can add new key-value pairs or change existing ones very easily.
- To add a new pair, just assign a value to a new key.
- To change an existing value, assign a new value to an existing key.
my_friend = {"name": "Charlie", "age": 28}
my_friend["occupation"] = "Engineer" # Add new
my_friend["age"] = 29 # Change existing
print(my_friend)To remove a pair, you can use the del keyword.
my_friend = {"name": "David", "age": 35, "city": "London"}
del my_friend["city"]
print(my_friend)Dictionary methods
Dictionaries come with helpful built-in functions, called methods, to do common tasks.
keys(): Gives you all the keys in the dictionary.values(): Gives you all the values in the dictionary.items(): Gives you all the key-value pairs.get(key, default): Safely retrieves a value. If the key isn't found, it returnsNoneor your specifieddefaultvalue instead of an error.
student = {"name": "Eve", "grade": "A"}
print(student.keys())
print(student.values())
print(student.items())
# Using get()
print(student.get("grade"))
print(student.get("school", "Unknown"))Nested dictionaries
You can put dictionaries inside other dictionaries. This is great for organizing more complex data.
team = {
"player1": {"name": "Frank", "jersey": 10},
"player2": {"name": "Grace", "jersey": 7}
}
print(team["player1"]["name"])
print(team["player2"]["jersey"])Common mistakes
- Forgetting the commas between key-value pairs.
- Mismatched curly braces
. - Trying to access a non-existent key without using
get(). - Using mutable types (like lists) as keys. Keys must be immutable!
KeyError, it means the key you're looking for doesn't exist in the dictionary. Double-check your key spelling or use .get()!Practice
Try these exercises to lock in what you just learned.
1d = {}23print(d)
What's next?
Now that you understand dictionaries, try using them to store more complex information. Think about how you would represent an inventory, a user profile, or even a simple address book.
Practicing with dictionaries will make you more comfortable with organizing data in Python, which is a super important skill!