Python Dictionary update()

Python dictionary update() method

Python dictionary update() method updates this dictionary with the items in the given iterable.

In this tutorial, you will learn about dictionary update() method in Python, and its usage, with the help of example programs.

Syntax of dict update()

The syntax of dictionary update() method is

dictionary.update(iterable)

Parameters

ParameterDescription
iterableRequired
An iterable object with key-value pairs. Could be another dictionary, or
Python dictionary update() method parameters

Return value

The dictionary update() method returns None.

Examples for update() method

1. Updating the dictionary with items from another dictionary using update() method in Python

In this example, we are given two dictionaries: my_dict, and other_dict; where both the dictionaries are initialized with some items. We have to update the dictionary my_dict with the items from the dictionary other_dict.

Call update() method on the given dictionary object my_dict, and pass the dictionary other_dict as argument.

The keys that are already present in my_dict shall be updated with the corresponding values from other_dict.

The keys that are not present in my_dict shall be inserted from other_dict.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

other_dict = {
    'bar': 55, # already present in original dictionary, therefore this value is considered for update
    'kee': 18  # not present in original dictionary, therefore inserted to original dictionary
}

my_dict.update(other_dict)
print(my_dict)
Run Code Copy

Since the key ‘bar’ is already present in the dictionary, its value is updated with 55 from other_dict. And for the key ‘kee’ that is not present in my_dict, the item is inserted to my_dict from other_dict.

Output

{'foo': 12, 'bar': 55, 'moo': 16, 'kee': 18}

2. Updating the dictionary with items from list of tuples using update() method in Python

In this example, we are given a dictionary my_dict, and a list of tuples list_1. The list of tuples list_1 is a list where each item in it is a tuple of key and value. We have to update the dictionary my_dict with the items from the list list_1.

Call update() method on the given dictionary object my_dict, and pass the list of tuples list_1 as argument.

The items in my_dict are updated or new items are inserted, just the same way as it was done in the previous example.

Python Program

my_dict = {
    'foo':12,
    'bar':14,
    'moo':16
}

list_1 = [
    ('bar', 55),
    ('kee', 18)
]

my_dict.update(list_1)
print(my_dict)
Run Code Copy

Output

{'foo': 12, 'bar': 55, 'moo': 16, 'kee': 18}

We are given with tuples as inner elements representing key-value pairs. Instead of tuples, we can take lists of length two to represent the items. But the condition is that in an item, the first element represents key, and the second element represents value.

Summary

In this tutorial of Python Dictionary Methods, we learned how to use Dictionary update() method to update the items in the dictionary from another dictionary or an iterable, with help of well detailed Python programs.

Related Tutorials

Code copied to clipboard successfully 👍