Updating the contents of a dictionary is a common operation when working with dictionaries.
For instance, suppose you have a dictionary that stores a list of car prices. If you want to apply a discount to one of the cars in the dictionary, you would want to update an item in your dictionary.
That’s where the Python dictionary update()
method comes in. This tutorial will discuss, with reference to an example, how to use the dictionary update()
method to update an item in a Python dictionary.
Python Dictionaries: A Refresher
In Python, dictionaries are referred to as the mapping data type.
This is because dictionaries add keys to values and create a key-value pair, which is used to store related data. Here is an example of a dictionary in Python:
book = { 'title': 'The Great Gatsby', 'published': '1925', 'author': 'F. Scott Fitzgerald', 'in_stock': False }
This dictionary contains four keys (which appear to the left of the colons) and values (which appear to the right of the colons).
Now, suppose we want to change the value of the in_stock
item in our dictionary to True. We could do so using the dictionary update()
method.
Python Dictionary update()
The dictionary update()
method allows you to update the contents of a dictionary.
Here is the syntax for the update method:
dictionary_name.update(new_item)
The value new_item
should be a dictionary with key-value pairs that replaces an existing item in your dictionary.
If the new item you specify does not already exist in the dictionary, it will be added to the dictionary.
Update an Item in a Dictionary
Suppose we are the owner of a bookstore and we have a dictionary that stores information on the book The Great Gatsby
. We want to update the in_stock
item in this dictionary because we have just received a new delivery of this book.
We could update our dictionary using the following program:
book = { 'title': 'The Great Gatsby', 'published': '1925', 'author': 'F. Scott Fitzgerald', 'in_stock': False } new_stock = { 'in_stock': True } book.update(new_stock) print(book)
Our code returns:
{'title': 'The Great Gatsby', 'published': '1925', 'author': 'F. Scott Fitzgerald', 'in_stock': True}
Let’s break down our code. First, we have declared a dictionary called book
which stores information about the book The Great Gatsby
.
Then, we have declared a variable with the revision we want to make to our dictionary. In this case, we have created a dictionary that sets the value of in_stock
to True.
Next, we use the update()
method to push the contents of new_stock
to our book
list. This allows us to update new_stock
to be equal to True in our book
dictionary. Finally, we print out our revised dictionary using print(book).
Conclusion
The Python dictionary update()
method allows you to update the contents of a dictionary.
This tutorial discussed, with reference to an example, how to use the dictionary update()
method to update an item in a dictionary. Now you’re equipped with the knowledge you need to update the contents of a Python dictionary like a professional developer!
About us: Career Karma is a platform designed to help job seekers find, research, and connect with job training programs to advance their careers. Learn about the CK publication.