+ 1
Any one tell how to update dictionary in python?
Dict.update()overwriting my dict..
4 Antworten
+ 8
d = {1: "one", 2: "three"}
d1 = {2: "two"}
# updates the value of key 2 d.update(d1)
print(d) d1 = {3: "three"}
# adds element with key 3 d.update(d1)
print(d)
For better explanation you can read this post .
https://www.google.com/amp/s/www.geeksforgeeks.org/JUMP_LINK__&&__python__&&__JUMP_LINK-dictionary-update-method/amp/
+ 3
Thamarai Selvan
Does something like this work?
a = { 'a': 10, 'b': 20, 'c': 30 }
b = { 'x': 200, 'y': 250, 'z': 300 }
print( a, b, sep = '\n' )
a.update( b )
print( a )
0
You are right.now my doubt is how to adding the new dictionary to existing dictionary..could you?
0
update() method inserts the specified items to the dictionary. The specified items can be a dictionary, or an iterable object.
dic1 = {1:'a', 2:'b'}
dic2 = {2:'c', 3:'d'}
dic1.update(dic2)
print(dic1)
Same as,
dic = {1:'a', 2:'b'}
dic[2]='c'
dic[3]='d'
print(dic)
Output:
{1:'a', 2:'c', 3:'d'}