In Python, dictionaries are a data type that allow you to store and manipulate a collection of key-value pairs. In this article, we will focus on how to modify dictionaries in Python. We will cover topics such as adding and updating items, deleting items, and iterating over the items in a dictionary.
Adding and Updating Dictionary Items
To add an item to a dictionary, you can use the square bracket notation and assign a value to a new key. If the key already exists in the dictionary, the value will be updated with the new value.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
info["country"] = "United States"
print(info) # prints {"name": "John", "age": 30, "city": "New York", "country": "United States"}
info["age"] = 31
print(info) # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}
You can also use the “update()” method to add or update multiple items in a dictionary. The “update()” method takes a dictionary as an argument and adds the key-value pairs of the argument dictionary to the original dictionary. If a key already exists in the original dictionary, the value is updated with the value from the argument dictionary.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
info.update({"age": 31, "country": "United States"})
print(info) # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}
Deleting Dictionary Items
To delete an item from a dictionary, you can use the “del” statement and the key of the item you want to delete. For example:
info = {"name": "John", "age": 30, "city": "New York"}
del info["age"]
print(info) # prints {"name": "John", "city": "New York"}
You can also use the “pop()” method to delete an item from a dictionary and store its value in a variable. The “pop()” method takes the key of the item as an argument, and removes the item from the dictionary. If the key is not found in the dictionary, the “pop()” method returns a default value specified as an optional argument.
For Example:
info = {"name": "John", "age": 30, "city": "New York"}
age = info.pop("age", 0)
print(info) # prints {"name": "John", "city": "New York"}
print(age) # prints 30
Iterating Over Dictionary Items
To iterate over the items in a dictionary, you can use a “for” loop and the “items()” method. The “items()” method returns a tuple of the key-value pairs in the dictionary, which you can unpack into variables in the “for” loop.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
for key, value in info.items():
print(key, ":", value)
The output of this code will be:
name : John
age : 30
city : New York
Alternatively, you can use the “keys()” method to iterate over the keys in the dictionary, or the “values()” method to iterate over the values.
info = {"name": "John", "age": 30, "city": "New York"}
# iterating over keys
for key in info.keys():
print(key)
# iterating over values
for value in info.values():
print(value)
Conclusion
In this article, we learned how to modify dictionaries in Python by adding and updating items, deleting items, and iterating over the items in a dictionary. By understanding these concepts, you will be able to effectively manipulate dictionaries in your Python programs.
In our “Introduction to Python” course, we will cover these concepts in more depth and show you how to use them to solve real-world problems. We hope you will join us on this journey!
Exercises
Here are some exercises with solutions to help you practice what you just learned:
How do you add an item to a dictionary in Python?
To add an item to a dictionary in Python, you can use the square bracket notation and assign a value to a new key. If the key already exists in the dictionary, the value will be updated with the new value.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
info["country"] = "United States"
print(info) # prints {"name": "John", "age": 30, "city": "New York", "country": "United States"}
info["age"] = 31
print(info) # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}
How do you update multiple items in a dictionary in Python?
To update multiple items in a dictionary in Python, you can use the “update()” method. The “update()” method takes a dictionary as an argument and adds the key-value pairs of the argument dictionary to the original dictionary. If a key already exists in the original dictionary, the value is updated with the value from the argument dictionary.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
info.update({"age": 31, "country": "United States"})
print(info) # prints {"name": "John", "age": 31, "city": "New York", "country": "United States"}
How do you delete an item from a dictionary in Python?
To delete an item from a dictionary in Python, you can use the “del” statement and the key of the item you want to delete. For example:
info = {"name": "John", "age": 30, "city": "New York"}
del info["age"]
print(info) # prints {"name": "John", "city": "New York"}
You can also use the “pop()” method to delete an item from a dictionary and store its value in a variable. The “pop()” method takes the key of the item as an argument.
How do you delete an item from a dictionary and return a default value if the key is not found in the dictionary in Python?
To delete an item from a dictionary and return a default value if the key is not found in the dictionary in Python, you can use the “pop()” method. The “pop()” method takes the key of the item as an argument, and removes the item from the dictionary. If the key is not found in the dictionary, the “pop()” method returns a default value specified as an optional argument.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
age = info.pop("age", 0)
print(info) # prints {"name": "John", "city": "New York"}
print(age) # prints 30
country = info.pop("country", "United States")
print(info) # prints {"name": "John", "city": "New York"}
print(country) # prints "United States"
How do you iterate over the items in a dictionary in Python?
To iterate over the items in a dictionary in Python, you can use a “for” loop and the “items()” method. The “items()” method returns a tuple of the key-value pairs in the dictionary, which you can unpack into variables in the “for” loop.
For example:
info = {"name": "John", "age": 30, "city": "New York"}
for key, value in info.items():
print(key, ":", value)
The output of this code will be:
name : John
age : 30
city : New York
Alternatively, you can use the “keys()” method to iterate over the keys in the dictionary, or the “values()” method to iterate over the values.
info = {"name": "John", "age": 30, "city": "New York"}
# iterating over keys
for key in info.keys():
print(key)
# iterating over values
for value in info.values():
print(value)