Dictionaries are quite useful and commonly used in python. It may happen that you require to add new keys or update an existing one in a dictionary. In this tutorial, we’ll look at how you can add or update items in a dictionary.
Before we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. For more, check out our guide on dictionaries and other data structures in python.
How to add or update items in a dictionary ?
You can use the subscript notation, which is, accessing the key or creating a new one using square brackets []
and then providing the corresponding value. Also, you can use the dictionary method update()
to add or update a key in an existing dictionary. There are other ways as well but for the purpose of this tutorial we’ll be limiting to these two are they are the common ones.
Using the subscript notation
The subscript notation is used to refer the value corresponding a key in a dictionary. For example, in the dictionary d = {'a':1, 'b':2}
, the subscript notation d['a']
will give you the value corresponding to the key 'a'
in the dictionary.
To add a new key to the dictionary you can simply use the subscript notation with the new key and assign its respective value using the assignment operator =
.
Example: Add a new key to the dictionary using subscript notation
# add item to a dictionary
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# print
print("Shares in your portfolio:", shares)
# add 80 shares of TSLA to the portfolio using subscript notation
shares['TSLA'] = 80
# print
print("Shares in your portfolio:", shares)
Output:
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
In the above example, the dictionary shares
stores the number of shares of different companies in a sample portfolio. The key represents the company and the value represents the number of shares of the company in the portfolio. A new key TSLA
is added to the dictionary with the value 80
using the subscript notation.
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
To update an existing key you can simply refer to it using the subscript notation and they change its value.
Example: Update an existing key using the subscript notation
# update item in a dictionary
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# print
print("Shares in your portfolio:", shares)
# update the shares of 'GOOG' to 150
shares['GOOG'] = 150
# print
print("Shares in your portfolio:", shares)
Output:
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 150}
In the above example, the existing value corresponding to the key 'GOOG'
is changed to 150
using the subscript notation.
The subscript notation, that is accessing and changing keys using the square brackets []
is a simple and intuitive way of adding or updating a dictionary.
Using the update() function
update()
is a dictionary function in python used to update a dictionary by changing the values of existing keys or adding new keys.
Example: Add a new key to the dictionary using the update function
# add item to a dictionary
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# print
print("Shares in your portfolio:", shares)
# add 80 shares of TSLA to the portfolio using the update function
shares.update({'TSLA': 80})
# print
print("Shares in your portfolio:", shares)
Output:
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
In the above example, the new key TSLA
with the value 80
is added to the dictionary using the dictionary function update()
. Note that the update()
function modifies the dict in-place.
Example: Update an existing key using the update function
# add item to a dictionary
# dictionary of a sample portfolio
shares = {'APPL': 100, 'GOOG': 50}
# print
print("Shares in your portfolio:", shares)
# update the shares of 'GOOG' to 150
shares.update({'GOOG': 150})
# print
print("Shares in your portfolio:", shares)
Output:
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 150}
In the above example, the update()
function is used to update the value of the existing key 'GOOG'
to 150
.
For more on the update function refer to the python docs.
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.