How can I include new entries in a Python dictionary?
In this initial section, I will provide an introduction.
The dictionary is a Python data type that comes pre-installed. It consists of key-value pairs and can be modified, although its keys must remain unchangeable and unique within each dictionary. Although there is no specific built-in method for adding elements to a dictionary, there exist multiple ways to do so. This article will cover the usage of the Python assignment operator, the update() method, and the merge and update dictionary operators for adding and updating elements in Python dictionaries.
Info
Using the = assignment operator to add an entry to a dictionary.
To include a new key in a dictionary, you have the option to utilize the = assignment operator.
dict[key] = value
If a dictionary already contains a certain key, the assignment operator will replace the current value with a new one.
This example illustrates the process of creating a new dictionary and then utilizing the assignment operator = to modify a value and include new 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 result is:
original dictionary: {‘a’: 1, ‘b’: 2} updated dictionary: {‘a’: 100, ‘b’: 2, ‘c’: 3, ‘d’: 4}
The result indicates that the original value of ‘a’ is replaced with a new value, the value of ‘b’ remains the same, and additional key-value pairs for ‘c’ and ‘d’ are included.
To expand the Dictionary without replacing existing values.
If you want to avoid replacing the original values of existing keys, you can use an if statement to conditionally add values instead of using the = assignment operator.
Building off the previous section’s example, if statements can be employed to exclusively include 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)
Here is the result.
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 result indicates that the value of c remained unchanged due to the conditional update of the dictionary.
Adding new entries to a dictionary by utilizing the update() method.
The update() method allows you to add a dictionary or a collection of key-value pairs to an existing dictionary. By using this method, you can replace the values of existing keys with the new values.
To illustrate, the given example showcases the process of generating a fresh dictionary, incorporating a new key-value pair and dictionary with the update() method, and displaying the outcome of each step.
site = {'Website':'Silicon Cloud', '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 result is:
original dictionary: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’} updated with Author: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy Shark’} updated with new dictionary: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy Shark’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}
The result indicates that the initial update introduces a fresh pair of key and value, while the subsequent update incorporates the key-value pairs from the guest dictionary to the site dictionary. Keep in mind that if a dictionary update includes an already existing key, the previous value is replaced by the update.
Using the merge operator to add to a dictionary
You have the ability to merge two dictionaries using the dictionary merge operator, which is symbolized by the pipe character. This will result in a new dictionary being returned.
This example shows how to create two dictionaries and merge them using the merge operator to create a new dictionary with all the key-value pairs.
site = {'Website':'Silicon Cloud', '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 result is:
site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’} guests: {‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’} new_site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}
The key-value pairs from the two dictionaries were combined to create a new dictionary object.
If there is a common key in both dictionaries, the value from the second dictionary, or the right operand, is the selected value. In the given code example, both dictionaries contain a key named ‘b’.
dict1 = {'a':'one', 'b':'two'}
dict2 = {'b':'letter two', 'c':'letter three'}
dict3 = dict1 | dict2
print{"dict3: ", dict3}
The result is:
dict3: {‘a’: ‘one’, ‘b’: ‘letter two’, ‘c’: ‘letter three’}
The value of key b got replaced by the value given in dict2.
Adding to a dictionary using the update |= operator.
To update a dictionary in-place with the provided dictionary or values, utilize the dictionary update |= operator, which is symbolized by the combination of the pipe and equal sign.
Similar to the merge operator, the update operator |= assigns the value from the right operand when a key is present in both dictionaries.
This example illustrates the process of creating two dictionaries, utilizing the update operator to add the second dictionary to the first one, and finally displaying the modified dictionary.
site = {'Website':'Silicon Cloud', 'Tutorial':'How To Add to a Python Dictionary', 'Author':'Sammy'}
guests = {'Guest1':'Dino Sammy', 'Guest2':'Xray Sammy'}
site |= guests
print("site: ", site)
Here is one possible paraphrase:
The result is:
site: {‘Website’: ‘Silicon Cloud’, ‘Tutorial’: ‘How To Add to a Python Dictionary’, ‘Author’: ‘Sammy’, ‘Guest1’: ‘Dino Sammy’, ‘Guest2’: ‘Xray Sammy’}
In the example before, there was no requirement to create a third dictionary object as the update operator alters the original object. The output reveals that the guests dictionary was added to the original site dictionary.
In summary, to conclude
You utilized various techniques in this article to enhance and modify a Python dictionary. Expand your knowledge further by exploring additional Python tutorials.
more tutorials from Silicloud
Set in Python(Opens in a new browser tab)
Addition Assignment Operator mean in Java(Opens in a new browser tab)
The Python functions ord() and chr()(Opens in a new browser tab)