The update dictionary python is a built-in function in the python programming language that updates the specific items into the dictionary. The items given by the user are iterable in the form of either a dictionary or an object with key-value pairs.
Syntax:
dictionary.update(iterable)
update() Parameters
The parameters given in the update() are iterable. The parameters can either be in the form of a dictionary or a tuple. If the update dictionary python is passed without a parameter, then the value of the dictionary does not change.
Return Value from update()
The update() in python will return the updated value of the dictionary with the parameters provided by the user.
For example:
In the example given below, we update the list of tuples to the dictionary using the update().
Source Code:
phone = {"brand": "Apple","model": "12 pro","year": 2020}phone.update({"color": "Blue"})print(phone)Output:{'brand': 'Apple', 'model': '12 pro', 'year': 2020, 'color': 'Clue'}
Let us see more examples to understand how the update dictionary python works:
Example 1: Working of update()
abc = {p: "one", q: "three"}abc1 = {q: "two"}# the value of key q is updated with the new valueabc.update(abc1)print(abc)abc1 = {r: "three"}# key r is added to dictionaryabc.update(abc1)print(abc)Output{p: 'one', q: 'two'}{p: 'one', q: 'two', r: 'three'}
Example 2: update() When Tuple is Passed
In the example given below, we pass tuples to the dictionary
Source Code:
az = {'x': two}az.update(y = three, z = 0)print(az)Output{'x': two, 'y': three, 'z': 0}
How do I update a dictionary in python?
Using the update dictionary python, we can update the dictionary values in python. If the element given by the user is not present inside the dictionary, the update() adds that element to the dictionary. If the element is already present and the user wants to update the value of that particular element, this can be done using the update().
How do you increment a dictionary in python?
In python, the dictionaries can be incremented using the get().
For example:
dict11 = {'a' : 1, 'b' : 2, 'c' : 4, 'd' : 5}print("The original dictionary provided was: " + str(dict11))# Using get()dict11['e'] = dict11.get('e', 0) + 3# printing resultprint("Incremented dict is: " + str(dict11))Output :The original dictionary provided was: : {'a' : 1, 'b' : 2, 'c' : 4, 'd' : 5}Incremented dict is:: {‘b’: 2, ‘d’: 5, ‘fc’: 4, ‘e’: 3, ‘a: 1}
How do you update a list in Python?
The user can update the lists in python using the update method after converting the lists into sets.
For example:
Source Code:
l1 = [1, 2, 3, 4]l2 = [1, 4, 2, 3, 5]alphabets11 = {'p', 'q', 'r'}# lists converted to setsset11 = set(l2)set22 = set(l1)# Update the methodset11.update(set22)# Print the updated setprint(set11)set11.update(alphabets)print(set11)Output :{1, 2, 3, 4, 5}{1, 2, 3, 4, 5, 'r', 'q', 'p'}
How do you match two dictionaries in Python?
In python, we can see whether the values of two dictionaries are matching with the help of the ‘==’ operator.
Source Code:
d1 = {'Name': 'amy', 'Age': 59}d2 = {'Name': 'lily', 'Age': 28}if d1 == d2:print "d1 and d2 have same values"else:print "d1 and d2 do not have the same values"Output:d1 and d2 do not have the same values