Python dictionary update method – Python Dictionary: Update() Function Tutorial and Examples

Python dictionary update method: Python’s implementation of an associative array, which is a data structure, is dictionaries. A dictionary is a collection of key-value pairs. Each key-value pair represents a key and its associated value.

Enclosing a comma-separated list of key-value pairs in curly braces defines a dictionary. A colon ‘ : ‘ separates each key from its associated value.

In this post, we’ll look at how to use the update() method of the dict class in Python, as well as some examples of how to use it.

Tutorial and Examples for the update() function

The update() method in Python Dictionary updates the dictionary with elements from another dictionary object or an iterable of key/value pairs.

Syntax:

dictioanry_name.update(iterable)

Parameters:

 As parameters, this method accepts a dictionary or an iterable object of key/value pairs (typically tuples).

Return:

It does not return a value, but instead updates the Dictionary with elements from a dictionary object or an iterable object of key/value pairs.

If a key appears in the sequence argument but does not exist in the dictionary, it is added to the dictionary along with the given value. If the update() function is called without an argument, the dictionary is not modified.

Updating the value of a key in a Python dictionary

In the python dictionary, just build a temporary dictionary containing the new value key and move it to update() function for the update value.

Below is the implementation:

# Given Dictionary
dictionary = {'hello': 50, 'this': 100, 'is': 200, 'BTechGeeks': 300}
# updating the value of hello key to 400
dictionary.update({'hello': 400})
# printing the updated dictionary
print("Updated Dictionary : ", dictionary)

Output:

Updated Dictionary :  {'hello': 400, 'this': 100, 'is': 200, 'BTechGeeks': 300}

Using update function if key is not in dictionary

If the update() function is passed a key-value pair and the given key does not exist in the dictionary, it creates it with the given value.

Below is the implementation:

# Given Dictionary
dictionary = {'hello': 50, 'this': 100, 'is': 200, 'BTechGeeks': 300}
# updating the new key using update() function
dictionary.update({'python': 500})
# printing the updated dictionary
print("Updated Dictionary : ", dictionary)

Output:

Updated Dictionary :  {'hello': 50, 'this': 100, 'is': 200, 'BTechGeeks': 300, 'python': 500}

Updating Multiple keys in dictionary

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

Below is the implementation:

# Given Dictionary
dictionary = {'hello': 50, 'this': 100, 'is': 200, 'BTechGeeks': 300}
# updating the multiple keys using update() function
dictionary.update({'python': 500, 'this': 150, 'is': 250})
# printing the updated dictionary
print("Updated Dictionary : ", dictionary)

Output:

Updated Dictionary :  {'hello': 50, 'this': 150, 'is': 250, 'BTechGeeks': 300, 'python': 500}

Update/Modify the key name in python dictionary

A dictionary’s key cannot be modified. So, if we want to change the key name in the dictionary, we must remove the current key-value pair from the dictionary and replace it with a new key that has the same value.

Approach:

  • Using the pop function, remove the key and save the value of the current key in a variable.
  • Make a new key with the above value and a new name(key).

Below is the implementation of above approach:

# Given Dictionary
dictionary = {'hello': 50, 'this': 100, 'is': 200, 'BTechGeeks': 300}
# storing value of any key say hello in keyvalue variable after removing it
keyvalue = dictionary.pop('hello')
# updating the new key with the above value
dictionary.update({'Helloworld': keyvalue})
# printing the updated dictionary
print("Updated Dictionary : ", dictionary)

Output:

Updated Dictionary :  {'this': 100, 'is': 200, 'BTechGeeks': 300, 'Helloworld': 50}

Related Programs: