Without creating a new dict
you can manipulate your current dict
with Counter
inside.
You can print your current items like this way:
dct = {'A': Counter({'food': 30, 'menu': 19, 'good': 15}), 'B': Counter({'one': 5, 'chicken': 10})}
for k, v in dct.items():
print(k, v)
Output:
>>> A Counter({'food': 30, 'menu': 19, 'good': 15})
>>> B Counter({'chicken': 10, 'one': 5})
In order to modify values inside Counter dict
, you can do something like this:
for k, v in dct:
if "food" in v:
v["food"] = 666
Output:
print(dct)
>>> {'A': Counter({'food': 666, 'menu': 19, 'good': 15}), 'B': Counter({'chicken': 10, 'one': 5})}
And if you want to add new elements to your Counter dict
you can do something like this:
for k, v in dct.items():
if "food" in v:
v.update({"new_food":0})
print(dct)
Output:
>>> {'A': Counter({'food': 30, 'menu': 19, 'good': 15, 'new_food': 0}), 'B': Counter({'chicken': 10, 'one': 5})}
Otherwise if you want to have a new "regular"
dict
you can use dict comprehension
:
new_dct = {k:{key:val for key,val in v.items()} for k,v in dct.items()}
print(new_dct)
>>> {'A': {'food': 30, 'menu': 19, 'good': 15}, 'B': {'chicken': 10, 'one': 5}}
for it1, it2 in values.items():
values
is one counter. You cannot split it intoit1
andit2
.