Ultimate Guide to the Python dictionary update() Function

A dictionary is a data structure in Python that is used to store key-value pairs. It is an unordered collection of items containing a key and value.

Python Dictionary update()

The dictionary update() method in Python adds new key-value pairs to a dictionary or updates the values of existing keys. 

If a key is not present in the dictionary, it first adds the key to the dictionary. After that, it combines the value; otherwise, if the key is already present in the dictionary, it updates the value.

Syntax

dict1.update(dict2)

Parameters

Here dict1 is the dictionary we want to update, and dict2 is the dictionary we want to copy.

Return Value

The dict.update() method does not return anything, and it just updates one dictionary with other dictionary objects or iterable keys/pairs.

Programming Example

# app.py

# Declaring two dictionaries
dict1 = {'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot'}
dict2 = {'Animal': 'Tiger', 'Tree': 'Coconut'}

# Printing dictionary1 and Dictionary2
print(dict1)
print(dict2)

# Now we will update dict1 with the value of dict2
dict1.update(dict2)

# Now printing new values of dictionary1
print(dict1)

Output

{'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot'}
{'Animal': 'Tiger', 'Tree': 'Coconut'}
{'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot', 'Animal': 'Tiger', 'Tree': 'Coconut'}

In the above program, we have declared two dictionaries, dict1 and dict2.

Then we printed all the keys and values of the dictionaries.

Then we updated dict1 with the objects of dict2, and finally, we printed them.

How to update a dictionary with an iterable in Python

Use the update() method to update a dictionary with an iterable of key-value pairs in Python. 

The update() method updates the dictionary with the key-value pairs from the iterable and returns None.

main_dict = {'Name': 'Krunal'}

print("Original dictionary:", main_dict)

main_dict.update(Rollno=21, College='VVP')

print("After updating a dictionary", main_dict)

Output

Original dictionary: {'Name': 'Krunal'}
After updating a dictionary {'Name': 'Krunal', 'Rollno': 21, 'College': 'VVP'}

You can see from the output that we updated an existing dictionary with an iterable using the dict.update() method.

How to update the dictionary if a key exists

If the key is already present in the dictionary, the value is updated with the new value. If the key is not present in the dictionary, it is added with the specified value.

To update the value of a key in a dictionary, if the key exists in Python, you can use either dict.update() method or the assignment operator(=).

main_dict = {"Name": "Krunal", "Rollno": 21, "College": "VVP"}

print("Original dictionary:", main_dict)

if "Rollno" in main_dict:
    main_dict.update(Rollno = 19)

print("After updating a dictionary", main_dict)

Output

Original dictionary: {'Name': 'Krunal', 'Rollno': 21, 'College': 'VVP'}
After updating a dictionary {'Name': 'Krunal', 'Rollno': 19, 'College': 'VVP'}

FAQs

How do I use the dict.update() function in Python?

To use the update() function in Python, you must call it on a dictionary object and pass it to another dictionary or an iterable of key-value pairs as an argument.

Can I use the update() function with other data types besides dictionaries?

Yes, you can use the dict.update() method with any iterable object that contains key: value pairs, such as lists of tuples or sets of dictionaries.

How do I update the key and value in the dictionary in Python?

To update the key and value of a dictionary in Python:

  1. Changing the value of the key directly.
  2. Using the update() method.
  3. Use the update() method with an iterable of key/value pairs.

Conclusion

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

You can also use the dict.update() function to update the dictionary with a list of tuples or a dictionary view object.

 

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.