Home > Enterprise >  How to add values of a single key in a dictionary
How to add values of a single key in a dictionary

Time:09-23

Say I have a dictionary:

a_dictionary = {"a": [1, 2], "b": [3, 4]}

I want to get a resultant dictionary that has the value:

a_dictionary = {"a": [3], "b": [7]}

Can someone please help me?

CodePudding user response:

Try this:

for k,v in a_dictionary.items():
    a_dictionary[k] = [sum(v)]

Output:

>>> print(a_dictionary)
{'a': [3], 'b': [7]}
  • Related