Post

Python Dictionary update()

In python dictionaries, the update() method is used to insert the specified key-value pairs to the dictionary from another dictionary or from an iterable.

Python Dictionary update()

The syntax of update() is:

1
2
dictionary.update(object)

update() Parameters

The update() method takes only one parameter as an argument:

  • object - Either a dictionary, iterable, or an object of key/value pairs.

Let’s see an example of the update() method in the dictionary.

###

Example 1: How to use the update() method on python dictionary?

1
2
3
4
5
6
car = {"brand": "Ford", "model": "Mustang", "year": 1964}

car.update({"color": "White"})

print(car)

Output:

1
2
{'brand': 'Ford', 'model': 'Mustang', 'year': 1964, 'color': 'White'}

Rules of update()

  • If the update method passed without any value it would return None.
  • It will only take values in key /value pairs.
This post is licensed under CC BY 4.0 by the author.