In this article discuss how to use the update() method of the dict class in python and then we will look at some examples of the update() function.

dict.update() Syntax:

In python, dict class provides a function to update the values of keys i.e.

dict.update([sequence])

Parameter:

      • Sequence: An optional iterable sequence of key-value pairs. It can be another dictionary or list of tuples etc.

Return Value:

      • None (It does not return any value)

update() function accepts an iterable sequence of key-value pairs (dictionary or list of tuples) as an argument and then updates the values of keys from the sequence to the dictionary.

If any key is present in sequence argument but not exist in the dictionary, then it adds the key in the dictionary along with the given value. Whereas, if the update() function is called without any argument, then it does not modify the dictionary.

Let’s understand more with some examples,

Examples of dict.update()

Update value of a key in a dictionary in python

To update the value of an existing key in the dictionary, just create a temporary dictionary containing the key with new value and then pass this temporary dictionary to the update() function,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# python dictionary update value of key
word_freq.update({'at': 100})

print('Modified Dictionary:')
print(word_freq)

Output:

Modified Dictionary:
{'Hello': 56,
 'at': 100,
 'test': 43,
 'this': 78}

It updated the value of the key ‘at in the dictionary.

The behavior of update() function, when passed a key that doesn’t exist in the dictionary

If we pass a key-value pair in the update() function and the given key does not exist in the dictionary, then it adds that key in the dictionary with the given value. For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# if key does not exist then upate(0 function
# will add a new key in dict with given value
word_freq.update({'here': 50})

print('Modified Dictionary:')
print(word_freq)

Output:

{'Hello': 56,
 'at': 23,
 'test': 43,
 'this': 78,
 'here': 50}

As key ‘here’ does not exist in the dictionary, so update() function added that too the dictionary.

Update values of multiple keys in a dictionary using update() function

If we want to update the values of multiple keys in the dictionary, then we can pass them as key-value pairs in the update() function. To bind keep multiple key-value pairs together, either we can use a list of tuples or we can create a temporary dictionary.

For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

# Update values of multiple keys in dictionary
word_freq.update({'here': 40,
                  'at': 41,
                  'test': 89})

print('Modified Dictionary:')
print(word_freq)

Output:

{'Hello': 56,
 'at': 41,
 'test': 89,
 'this': 78,
 'here': 40}

Here we passed 3 key-value pairs to the update() function. Out of these 3, the 2 keys exist in the dictionary and the third key i.e. ‘here’ doesn’t exist in the dictionary. So, it updated the values of 2 keys that already exist in the dictionary and added the third one in the dictionary.

Update the key name in dictionary using update() function

We can not change the key in a dictionary. So if we want to change the key in the dictionary then we need to delete the current key-value pair from the dictionary and add a new key in the dictionary with the same value.

For example,

# Dictionary of string and int
word_freq = {
    "Hello": 56,
    "at": 23,
    "test": 43,
    "this": 78
}

#Update key name in python dictionary
value = word_freq.pop('at')
word_freq.update({'where': value})

print('Modified Dictionary:')
print(word_freq)

Output:

{'Hello': 56,
 'test': 43,
 'this': 78,
 'where': 23}

It gave an effect that we have updated the key name from ‘at’ to ‘where’. But actually, we fetched the value of key ‘at’, then deleted that entry from the dictionary and then added a new key ‘where’ in the dictionary with the same value.

So, this is how we can use the update() method of the dict class in python to add or update values.

Join a list of 2000+ Programmers for latest Tips & Tutorials