Dictionaries are used to store data using the key-value structure in Python.
When you’re working with dictionaries, you may ask yourself, how can I add an item to a dictionary? This article will answer that question.
We’ll break down the basics of dictionaries, how they work, and how you can add an item to a Python dictionary. By the end of this tutorial, you’ll be an expert at adding items to Python dictionaries.
Python Dictionary: A Refresher
The dictionary data structure allows you to map keys to values. This is useful because keys act as a label for a value, which means that when you want to access a particular value, all you have to do is reference the key name for the value you want to retrieve.
Dictionaries generally store information that is related in some way, such as a record for a student at school, or a list of colors in which you can purchase a rug.
Consider the following dictionary:
scones = {
"Fruit": 22,
"Plain": 14,
"Cinnamon": 4,
"Cheese": 21
}
This dictionary contains four keys and four values. The keys are stored as a string, and appear to the left of the colon; the values are stored to the right of the colons; bound to a particular key in the dictionary.
Now, how do we add an item to this dictionary? Let’s break it down.
Python Add to Dictionary
To add an item to a Python dictionary, you should assign a value to a new index key in your dictionary.
Unlike lists and tuples, there is no add()
, insert()
, or append()
method that you can use to add items to your data structure. Instead, you have to create a new index key, which will then be used to store the value you want to store in your dictionary.
Here is the syntax for adding a new value to a dictionary:
dictionary_name[key] = value
Let’s use an example to illustrate how to add an item to a dictionary. In our dictionary from earlier, we stored four keys and values. This dictionary reflected the stock levels of different flavors of scones sold at a cafe.
Suppose we have just baked a batch of 10 new cherry scones. Now, we want to add these scones to our dictionary of scone types. To do so, we can use this code:
scones = {
"Fruit": 22,
"Plain": 14,
"Cinnamon": 4,
"Cheese": 21
}
scones["Cherry"] = 10
print(scones)
Our code returns:
{'Fruit': 22, 'Plain': 14, 'Cinnamon': 4, 'Cheese': 21, 'Cherry': 10}
As you can see, our code has added the “Cherry” key to our dictionary, which has been assigned the value 10.
In our code, we first declared a dictionary called “scones” which stores information on how many scones are available at a cafe to order. Next, we added the key “Cherry” to our dictionary and assigned it the value 10 by using the following code:
scones["Cherry"] = 10
Finally, we printed out the updated version of our dictionary.
Notice that, in the output of our code, our dictionary is not ordered. This is because data stored in a dictionary, unlike data stored in a list, does not have any particular order.
Tip: Adding and Updating Use the Same Notation
The same notation can be used to update a value in a dictionary. Suppose we have just baked a new batch of 10 cinnamon scones. We could update our dictionary to reflect this using the following line of code:
scones = {
"Fruit": 22,
"Plain": 14,
"Cinnamon": 4,
"Cheese": 21
}
scones["Cinnamon"] = 14
print(scones)
Our code returns:
{'Fruit': 22, 'Plain': 14, 'Cinnamon': 14, 'Cheese': 21}
This line of code sets the value associated with the “Cinnamon” key in our dictionary to 14.
Conclusion
There is no add()
, append()
, or insert()
method you can use to add an item to a dictionary in Python. Instead, you add an item to a dictionary by inserting a new index key into the dictionary, then assigning it a particular value.
This tutorial discussed, with an example, how to add an item to a Python dictionary. Now you’re ready to start adding items to dictionaries like a Python expert!
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.