-2

Let's say I have three different dictionaries:

dictA = {'A': 1, 'B': 2, 'C': 3} dictB = {'C': 1, 'D': 2, 'E': 3} dictC = {'A': 2, 'C': 4, 'E': 6, 'G': 8} 

And I want to "add" these dictionaries together, in the dictA+dictB+dictC order. What I mean by that is:

  • values with the same keys will be added together (for example 'A': 1 in dictA and 'A': 2 in dictB will become 'A': 3 in the end)

  • values without a previously existing key will be created (for example during dictB + dictC, the key 'G' does not exist, so it will be created)

The result for this example should look something like this:

resultDict = {'A': 3, 'B': 2, 'C': 8, 'D': 2, 'E': 9, 'G': 2,} 

Is there an easy way to do this? The dictionaries I am actually working with are much larger and nested in multiple other dictionaries, so sorry if this example isn't well explaining. I tried fidgeting around with for loops but as I mentioned, the lists I am actually working with are much larger and not that easy to work with.

4
  • Sounds like you want collections.Counter Commented Jul 29, 2024 at 1:23
  • I would define an empty result dictionary, then iterate over all keys of all input dictionaries and add their values to the corresponding key in result. IMO this is easy. What is your definition of "easy"? Commented Jul 29, 2024 at 1:27
  • Iterate over the keys in dict B. If that key exists in dict A, add the B value to the A key, otherwise create the new key in dict A. What is the actual difficulty here? Commented Jul 29, 2024 at 1:34
  • Can you give an example of the expected output when you have a nested dictionary? Commented Jul 29, 2024 at 2:19

2 Answers 2

0

collections Counter makes this very simple since you can add Counter objects together. In other words, this does what you want:

Counter(dictA) + Counter(dictB) + Counter(dictC) 

If you want to generalize, you can use sum() and provide an empty Counter for the start parameter, so make the very succinct:

from collections import Counter dictA = {'A': 1, 'B': 2, 'C': 3} dictB = {'C': 1, 'D': 2, 'E': 3} dictC = {'A': 2, 'C': 4, 'E': 6, 'G': 8} dicts = [dictA, dictB, dictC] counts = sum(map(Counter, dicts), Counter()) # Counter({'E': 9, 'C': 8, 'G': 8, 'A': 3, 'B': 2, 'D': 2}) 

A counter object acts just like a dictionary, but if you specifically need a dict, you can just use dict(counts).

Sign up to request clarification or add additional context in comments.

1 Comment

Definitely more readable than mine, upvoted.
0

An easy way using the format you showed, is to collect all the keys then .get() each key with a default of 0 and add into a new dictionary.

Here is that basic structure that shows it in action:

dictA = {'A': 1, 'B': 2, 'C': 3} dictB = {'C': 1, 'D': 2, 'E': 3} dictC = {'A': 2, 'C': 4, 'E': 6, 'G': 8} dict_list = [dictA, dictB, dictC] # Collect all the keys from each dict all_keys = set() for dictx in dict_list: all_keys.update(dictx.keys()) # Iterate through each key adding it to the result resultDict = {} for key in all_keys: # Pre-init with a 0 then grab it or default to 0 from each dict resultDict[key] = 0 for dictx in dict_list: resultDict[key] += dictx.get(key, 0) print(resultDict) 

Here is a sample code condensed to a oneliner:

resultDict = {key: sum([x.get(key, 0) for x in dict_list]) for key in set().union(*(d.keys() for d in dict_list))} 

Mainly inspired by this answer.

Let me know if you have any questions.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.