Python add or update key in a dictionary banner

Python Add or Update Item in Dictionary

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.

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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top