Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

Python Dictionary update() Method

  • 01 Sep, 2025
  • Com 0
Python Dictionary update() Method

Python Dictionary update() method updates the dictionary with the items from another dictionary, an iterable of key-value pairs, or keyword arguments into the calling dictionary.

If a key already exists, its value will be overwritten with the new value; if not, a new key-value pair is added.

Updating a dictionary in Python

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

print("Original dictionary1: ", dict1)

# Output: {'Flower': 'Rose', 'Fruit': 'Apple', 'Bird': 'Parrot'}

print("Original dictionary2: ", dict2)

# Output: {'Animal': 'Tiger', 'Tree': 'Coconut'}

dict1.update(dict2)

print("Updated dictionary1: ", dict1)

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

In Python 3.9, the | operator was introduced, which creates a new dictionary by merging (e.g., d1 | d2), whereas update() is an in-place operation, similar to d1 |= d2.

Use update() for efficiency when the original reference must be preserved.

Syntax

dict1.update(iterable)

Parameters

Argument Description
iterable(optional)

It represents a dictionary or an iterable object with key/value pairs.

If it is an iterable of tuples or lists, each element is a 2-item sequence (key, value), e.g., [(‘a’, 1), (‘b’, 2)].

Updating a dictionary with key-values

Updating a dictionary with key-values in Python

In the above figure, we defined a dictionary with one element and used the update() method to add new key-value elements and change the existing property as well.

The update() method can accept keyword arguments directly to add/overwrite string-key values to an existing dictionary.

main_dict = {'Name': 'Krunal'}

print("Original dictionary:", main_dict)

# Output: Original dictionary: {'Name': 'Krunal'}

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

print("After updating a dictionary", main_dict)

# Output: After updating a dictionary {'Name': 'Ankit', 'Rollno': 21, 'College': 'VVP'}

In this code, we updated the main_dict with three key-value pairs. The first property is Name=Krunal.

We are now updating this property with the name=Ankit and adding two new key-value pairs: Rollno=21 and College=VVP.

So, the final updated main_dict contains three elements.

Non-2-Item Iterable

If you attempt to use a non-2-Item Iterable in the update() method, it will throw an error.

dict = {}

try:
    dict.update([('a', 1), ('b')])  # Second item has length 1
except ValueError as e:
    print(e)

# Output: dictionary update sequence element #1 has length 1; 2 is required

Updating the dictionary if a key exists

Updating the dictionary if a key exists

What if the key already exists while updating a dictionary? In that case, it will override the existing values with new ones.

origin_dict = {'a': 1, 'b': 2}

print(origin_dict)

# Output: {'a': 1, 'b': 2}

new_dict = {'a': 0, 'b': 0}

origin_dict.update(new_dict)

print(origin_dict)

# Output: {'a': 0, 'b': 0}

Using a tuple iterable

We can add new keys from the list of tuples using the update() method, which works with any iterable that yields 2-item sequences.

dict = {'x': 21}

list_of_tuples = [('y', 31), ('z', 0)]

dict.update(list_of_tuples)

print(dict)

# Output: {'x': 21, 'y': 31, 'z': 0}

Updating an empty dictionary

Updating an empty dictionary

If the input dictionary is empty, we can update it with new values as they are populated efficiently.

empty_dict = {}

print(empty_dict)

# Output: {}

filled_dict = {'name': 'KDL'}

empty_dict.update(filled_dict)

print(empty_dict)

# Output: {'name': 'KDL'}

That’s it.

Post Views: 7
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Convert JSON to CSV in Python
JavaScript Array shift() Method

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend