Changing Dictionaries in Python
Dictionaries in Python are mutable, meaning you can change their content after they have been created. This includes adding, modifying, and removing key-value pairs. Understanding how to manipulate dictionaries is essential for effective data management in Python.
Adding Items to a Dictionary
You can add new key-value pairs to a dictionary by assigning a value to a new key. If the key already exists, the value will be updated.
# Create a dictionary
my_dict = {"name": "John", "age": 30}
# Add a new key-value pair
my_dict["city"] = "New York"
# Output: {"name": "John", "age": 30, "city": "New York"}
print(my_dict)
In the example above, the key "city" is added to the dictionary with the value "New York". If "city" were already a key, its value would be updated to "New York".
Modifying Items in a Dictionary
To modify the value of an existing key, simply assign a new value to that key.
# Create a dictionary
my_dict = {"name": "John", "age": 30}
# Modify an existing key-value pair
my_dict["age"] = 31
# Output: {"name": "John", "age": 31"}
print(my_dict)
Here, the value of the key "age" is updated from 30 to 31.
Removing Items from a Dictionary
Items can be removed from a dictionary using the del
statement or the pop()
method. The del
statement removes a key-value pair by key, while pop()
can remove a key-value pair and return its value.
# Create a dictionary
my_dict = {"name": "John", "age": 30, "city": "New York"}
# Remove an item using del
del my_dict["city"]
# Output: {"name": "John", "age": 30}
print(my_dict)
# Create another dictionary
my_dict = {"name": "John", "age": 30, "city": "New York"}
# Remove an item using pop and get the removed value
removed_value = my_dict.pop("city")
# Output: {"name": "John", "age": 30}
print(my_dict)
# Output: New York
print(removed_value)
In the example, the del
statement removes the key "city" from the dictionary. The pop()
method does the same but also returns the value associated with the removed key.
Clearing a Dictionary
If you want to remove all items from a dictionary, you can use the clear()
method.
# Create a dictionary
my_dict = {"name": "John", "age": 30, "city": "New York"}
# Clear all items from the dictionary
my_dict.clear()
# Output: {}
print(my_dict)
The clear()
method removes all key-value pairs from the dictionary, leaving it empty.
Merging Dictionaries
To merge two dictionaries, you can use the update()
method or the merge operator (|) in Python 3.9 and later.
# Create two dictionaries
dict1 = {"name": "John", "age": 30}
dict2 = {"city": "New York", "country": "USA"}
# Merge dictionaries using update
dict1.update(dict2)
# Output: {"name": "John", "age": 30, "city": "New York", "country": "USA"}
print(dict1)
# Merge dictionaries using the merge operator (Python 3.9+)
merged_dict = dict1 | dict2
# Output: {"name": "John", "age": 30, "city": "New York", "country": "USA"}
print(merged_dict)
In the example, the update()
method merges dict2
into dict1
. In Python 3.9 and later, the merge operator (|) creates a new dictionary combining both dictionaries.
Import Links
Here are some useful import links for further reading: