Python update() method updates the dictionary with the key and value pairs. It inserts key/value if it is not present. It updates key/value if it is already present in the dictionary. Show
It also allows an iterable of key/value pairs to update the dictionary. like: update(a=10,b=20) etc. Signature and examples of this method are given below. SignatureParametersother: It is a list of key/value pairs. ReturnIt returns None. Let?s see some examples of update() method to understand it?s functionality. Python Dictionary update() Method Example 1It is a simple example to update the dictionary by passing key/value pair. This method updates the dictionary. See an example below. Output: Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000} Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50} Python Dictionary update() Method Example 2If element (key/value) pair is already presents in the dictionary, it will overwrite it. See an example below. Output: Inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50} Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 50} Updated inventory: {'Fan': 200, 'Bulb': 150, 'Led': 1000, 'cooler': 150} Python Dictionary update() Method Example 3The update() method also allows iterable key/value pairs as parameter. See, the example below two values are passed to the dictionary and it is updated. ❮ Dictionary Methods ExampleInsert an item to the dictionary: car = { car.update({"color": "White"}) print(car) Try it Yourself »Definition and UsageThe The specified items can be a dictionary, or an iterable object with key value pairs. Syntaxdictionary.update(iterable) Parameter ValuesParameterDescriptioniterableA dictionary or an iterable object with key value pairs, that will be inserted to the dictionary❮ Dictionary Methods Dictionaries are quite useful and commonly used in python. It may happen that you require to add new keys or update an existing one in a dictionary. In this tutorial, we’ll look at how you can add or update items in a dictionary. Before we proceed, here’s a quick refresher on dictionaries in python – Dictionaries are a collection of items used for storing key to value mappings. They are mutable and hence we can update the dictionary by adding new key-value pairs, removing existing key-value pairs, or changing the value corresponding to a key. For more, check out our guide on dictionaries and other data structures in python. How to add or update items in a dictionary ?You can use the subscript notation, which is, accessing the key or creating a new one using square brackets Using the subscript notationThe subscript notation is used to refer the value corresponding a key in a dictionary. For example, in the dictionary 0, the subscript notation 1 will give you the value corresponding to the key 2 in the dictionary.To add a new key to the dictionary you can simply use the subscript notation with the new key and assign its respective value using the assignment operator 3.Example: Add a new key to the dictionary using subscript notation
Output:
In the above example, the dictionary 4 stores the number of shares of different companies in a sample portfolio. The key represents the company and the value represents the number of shares of the company in the portfolio. A new key 5 is added to the dictionary with the value 6 using the subscript notation.To update an existing key you can simply refer to it using the subscript notation and they change its value. Example: Update an existing key using the subscript notation
Output:
In the above example, the existing value corresponding to the key 7 is changed to 8 using the subscript notation.The subscript notation, that is accessing and changing keys using the square brackets Using the update() function
Example: Add a new key to the dictionary using the update function
Output:
In the above example, the new key 5 with the value 6 is added to the dictionary using the dictionary function update() . Note that the update() function modifies the dict in-place.Example: Update an existing key using the update function
Output:
In the above example, the 7 to 8.For more on the update function refer to Tutorials on python dictionaries –
Author
|