Introduction
A dictionary is a built-in Python data type. It is a sequence of key-value pairs, where each key is unique and maps to a value. Dictionaries are mutable objects, meaning you can change their contents without changing their identity. However, dictionary keys are immutable and must be unique within each dictionary. This makes dictionaries very efficient for searching, inserting, and deleting.
Dictionaries are widely used in Python for a variety of purposes, such as counting occurrences, grouping data, and storing settings. Despite their versatility, there is no built-in append method for dictionaries. Instead, there are several ways to append to and update a dictionary, each with its own uses and benefits.
In this article, you will learn different ways to add and update Python dictionaries. You will learn how to use Python's assignment operator, the update() method, and the dictionary merge and update operators. By the end of this tutorial, you will have a solid understanding of how to manage and manipulate dictionaries effectively in your Python programs.
Add to Python Dictionary Using = Assignment Operator
To add a new key to the dictionary you can use the = assignment operator:
dict[key] = value
If a key already exists in the dictionary, the assignment operator updates or overwrites the value.
The following example shows how to create a new dictionary and then use the assignment operator = to update a value and add key-value pairs:
dict_example = {'a': 1, 'b': 2}
print("original dictionary: ", dict_example)
dict_example['a'] = 100 # existing key, overwrite
dict_example['c'] = 3 # new key, add
dict_example['d'] = 4 # new key, add
print("updated dictionary: ", dict_example)The output is:
Output
original dictionary: {'a': 1, 'b': 2}
updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4}The output shows that the value of a is overwritten with the new value, the value of b is unchanged, and new key-value pairs are added for c and d.
Add values to a Python dictionary without overwriting
Using the assignment operator = overwrites the values of existing keys with the new values. If you know that your application may have duplicate keys, but you don't want to overwrite the original values, you can conditionally add values using an if statement.
Continuing the example from the previous section, you can use if statements to add only new key-value pairs to the dictionary:
dict_example = {'a': 1, 'b': 2}
print("original dictionary: ", dict_example)
dict_example['a'] = 100 # existing key, overwrite
dict_example['c'] = 3 # new key, add
dict_example['d'] = 4 # new key, add
print("updated dictionary: ", dict_example)
# add the following if statements
if 'c' not in dict_example.keys():
dict_example['c'] = 300
if 'e' not in dict_example.keys():
dict_example['e'] = 5
print("conditionally updated dictionary: ", dict_example)The output is:
Output
original dictionary: {'a': 1, 'b': 2}
updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4}
conditionally updated dictionary: {'a': 100, 'b': 2, 'c': 3, 'd': 4, 'e': 5}The output shows that because of the if condition, the value of c does not change when the dictionary is conditionally updated.
Adding to a Python dictionary using the update() method.
You can add a dictionary or a repeating key-value pair to a dictionary using the update() method. The update() method overwrites the values of existing keys with new values.
The following example shows how to create a new dictionary, use the update() method to add a new key-value pair and a new dictionary, and print each result:
site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary'}
print("original dictionary: ", site)
# update the dictionary with the author key-value pair
site.update({'Author':'Sammy Shark'})
print("updated with Author: ", site)
# create a new dictionary
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
# update the original dictionary with the new dictionary
site.update(guests)
print("updated with new dictionary: ", site)The output is:
Output
original dictionary: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary'}
updated with Author: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy Shark'}
updated with new dictionary: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy Shark', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}The output shows that the first update adds a new key-value pair and the second update adds key-value pairs from the guest dictionary to the site dictionary. Note that if the update to a dictionary contains an existing key, the old value is overwritten by the update.
Adding to Python Dictionary Using Merge | Operator
You can use the dictionary merge | operator, represented by the pipe character, to merge two dictionaries and return a new dictionary object.
The following example shows how to create two dictionaries and use the merge operator to create a new dictionary that contains key-value pairs from both:
site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
new_site = site | guests
print("site: ", site)
print("guests: ", guests)
print("new_site: ", new_site)The output is:
Output
site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy'}
guests: {'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}
new_site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}The two dictionaries were merged into a new dictionary object that contains the key-value pairs from both dictionaries.
If a key exists in both dictionaries, the value is taken from the second dictionary, or the right-hand operand. In the following example code, both dictionaries have a key named b:
dict1 = {'a':'one', 'b':'two'}
dict2 = {'b':'letter two', 'c':'letter three'}
dict3 = dict1 | dict2
print{"dict3: ", dict3}The output is:
Output
dict3: {'a': 'one', 'b': 'letter two', 'c': 'letter three'}The value of key b was overwritten with the value of the right-hand operand, dict2.
Add to Python dictionary using update |= operator
You can use the dictionary update operator |=, represented by the pipe and equal sign characters, to update a dictionary in place with the given dictionary or values.
Just like the merge | operator, if a key exists in both dictionaries, the update operator |= takes the value from the right operand.
The following example shows how to create two dictionaries, use the update operator to append the second dictionary to the first dictionary, and then print the updated dictionary:
site = {'Website':'DigitalOcean', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
site |= guests
print("site: ", site)The output is:
Output
site: {'Website': 'DigitalOcean', 'Tutorial': 'How To Add to a Python Dictionary', 'Author': 'Sammy', 'Guest1': 'Dino Sammy', 'Guest2': 'Xray Sammy'}
In the previous example, you didn't need to create a third dictionary object because the update operator modifies the original object. The output shows that the guest dictionary is appended to the site's main dictionary.
Result
In this article, you used different methods to add and update Python dictionaries.










One comment
Thank you.