How do you update an existing dictionary in Python?

Python update() method updates the dictionary with the key and value pairs. It inserts key/value if it is not present. It updates key/value if it is already present in the dictionary.

It also allows an iterable of key/value pairs to update the dictionary. like: update(a=10,b=20) etc.

Signature and examples of this method are given below.

Signature

Parameters

other: It is a list of key/value pairs.

Return

It returns None.

Let?s see some examples of update() method to understand it?s functionality.

Python Dictionary update() Method Example 1

It is a simple example to update the dictionary by passing key/value pair. This method updates the dictionary. See an example below.

Output:

Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}

Python Dictionary update() Method Example 2

If element (key/value) pair is already presents in the dictionary, it will overwrite it. See an example below.

Output:

Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50}
Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 150}

Python Dictionary update() Method Example 3

The update() method also allows iterable key/value pairs as parameter. See, the example below two values are passed to the dictionary and it is updated.

❮ Dictionary Methods


Example

Insert an item to the dictionary:

car = {
  "brand": "Ford",
  "model": "Mustang",
  "year": 1964
}

car.update({"color": "White"})

print(car)

Try it Yourself »


Definition and Usage

The update() method inserts the specified items to the dictionary.

The specified items can be a dictionary, or an iterable object with key value pairs.


Syntax

dictionary.update(iterable)

Parameter Values

ParameterDescriptioniterableA dictionary or an iterable object with key value pairs, that will be inserted to the dictionary

❮ Dictionary Methods


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

Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
0, the subscript notation
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
1 will give you the value corresponding to the key
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
2 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

Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
3.

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 in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
4 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
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
5 is added to the dictionary with the value
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
6 using the subscript notation.

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

Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
7 is changed to
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
8 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

Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
5 with the value
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
6 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

Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
7 to
Shares in your portfolio: {'APPL': 100, 'GOOG': 50}
Shares in your portfolio: {'APPL': 100, 'GOOG': 50, 'TSLA': 80}
8.

For more on the update function refer to

Tutorials on python dictionaries –

  • Python Dictionary Clear – With Examples
  • Python Add or Update Item in Dictionary
  • Python Dictionary Pop vs Popitem
  • Python view dictionary Keys and Values
  • Python Dictionary Items – With Examples
  • Python Dictionary Get – With Examples


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. In the past, he's worked as a Data Scientist for ZS and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

    How do you update a dictionary in Python?

    Python Dictionary update() method updates the dictionary with the elements from another dictionary object or from an iterable of key/value pairs..
    Syntax: dict.update([other]).
    Parameters: This method takes either a dictionary or an iterable object of key/value pairs (generally tuples) as parameters..

    How do you update an existing value in a dictionary?

    Values of a Python Dictionary can be updated using the following two ways i.e. using the update() method and also, using square brackets. Dictionary represents the key-value pair in Python, enclosed in curly braces. The keys are unique and a colon separates it from value, whereas comma separates the items.

    How do you add values to an existing dictionary in Python?

    To append an element to an existing dictionary, you have to use the dictionary name followed by square brackets with the key name and assign a value to it.

    Can dictionaries in Python be modified?

    Yes you can, dictionary is an mutable object so they can be modified within functions, but it must be defined before you actually call the function. To change the value of global variable pointing to an immutable object you must use the global statement.