+ 1

Any one tell how to update dictionary in python?

Dict.update()overwriting my dict..

5th Sep 2020, 2:14 AM
Thamarai Selvan
Thamarai Selvan - avatar
4 ответов
+ 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/
5th Sep 2020, 2:29 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 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 )
5th Sep 2020, 3:41 AM
Ipang
0
You are right.now my doubt is how to adding the new dictionary to existing dictionary..could you?
5th Sep 2020, 2:40 AM
Thamarai Selvan
Thamarai Selvan - avatar
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'}
5th Sep 2020, 6:24 AM
Jenson Y