Home > Software engineering >  Python3.. i want to update a dictionary by other dictionary's values
Python3.. i want to update a dictionary by other dictionary's values

Time:12-07

#guys i want to check if gg keys == x keys i will add the value from gg to x like this >>>

gg={2: 6, 4: 4, 6: 4, 7: 4, 8: 3}
x = {1:0, 2:0, 3:0....}

#i want to add number 6 to key 2 in x

from collections import Counter
arr = [2,2,2,2,2,2,4,4,4,4,6,6,6,6,7,7,7,7,8,8,8]
gg = Counter(arr)
val = gg.keys()
val_list = list(val)
x = dict.fromkeys(range(1,101),0)
values = x.values()
values_list = list(values)

CodePudding user response:

The question isn't completely clear. But from what I understand you want to update all the keys that are in x with the corresponding values in gg. That can be done by

x.update({i:j for i,j in gg.items() if i in x.keys()})
  • Related