-2

I want to take the elements of a list one by one and search them in 4 different dictionaries in python. then I want to create a new dictionary and put the elements of that list as keys and the values I found from those 4 dictionaries as values?

For example:

list = ['a', 'b', 'c', 'd', 'e']
dict1 = {'a': 10, 'b': 2, 'c': 45}
dict2 = {'a': 15, 'b': 55}
dict3 = {'a': 79, 'b': 6, 'c': 3}
dict4 = {'d': 600, 'e': 30}

The result I want:

newlist = {'a': [10, 15, 79, 0], 
'b': [2, 55, 6, 0], 
'c': [45, 0, 3, 0], 
'd': [0, 0, 0, 600], 
'e': [0, 0, 0, 30]}
FATMA
  • 21
  • 3
  • 4
    What happened when you tried to write code to solve the problem? – Karl Knechtel Jun 18 '21 at 15:41
  • 1
    That isn't "four different dictionaries", it's one dictionary which has 4 items. This is straightforward. – smci Jun 18 '21 at 15:42
  • 1
    You simply want `dict1.get(key, 0)` to return a default 0 value if key not found. – smci Jun 18 '21 at 15:44
  • do your 4 dictionaries share keys? – chatax Jun 18 '21 at 15:47
  • Now you've edited it to change one dictionary to four dictionaries. Please try to state the question correctly the first time, otherwise it invalidates answers people write you. – smci Jun 18 '21 at 16:22

2 Answers2

5

This dict comprehension will result in what you're looking for:

dicts = dict1, dict2, dict3, dict4
{k: [d.get(k, 0) for d in dicts] for k in list1}
Will Da Silva
  • 6,386
  • 2
  • 27
  • 52
  • This is a duplicate of [Return default value if Dictionary key is not available](https://stackoverflow.com/a/6130800/202229). – smci Jun 18 '21 at 16:19
2

Something like this?

from collections import defaultdict

list1 = ['a', 'b', 'c', 'd', 'e']
dict1 = {'a': 10, 'b': 2, 'c': 45}
dict2 = {'a':11, 'b':20, 'z':100}

def collect_values(list1, dictionaries):
    result = defaultdict(list)
    for key in list1:
        for d in dictionaries:
            result[key].append(d.get(key, 0))
    return result

print(collect_values(list1, [dict1, dict2]))

Which would return

defaultdict(<class 'list'>, {'a': [10, 11], 'b': [2, 20], 'c': [45, 0], 'd': [0, 0], 'e': [0, 0]})

EDIT

If appending zeros each time the key is not found in the dictionary is undesired behavior, the function below can be used:

def collect_values(list1, dictionaries):
    result = defaultdict(list)
    for key in list1:
        for d in dictionaries:
            if key in d.keys():
                result[key].append(d[key])
            elif not result[key]:
                result[key].append(0)
    return result

Which returns

defaultdict(<class 'list'>, {'a': [10, 11], 'b': [2, 20], 'c': [45], 'd': [0], 'e': [0]})
ChrisOram
  • 1,254
  • 1
  • 5
  • 17
  • I got this error : 'TypeError: first argument must be callable or None' – FATMA Jun 18 '21 at 16:06
  • This sounds like it's coming from the instantiation of the defaultdict, have you definitively passed `list` as the argument? – ChrisOram Jun 18 '21 at 16:13
  • In your question it looks like you overwrite the name `list` in your modules namespace, change `list = ['a', 'b', 'c', 'd', 'e']` to `list1 = ['a', 'b', 'c', 'd', 'e']` – ChrisOram Jun 18 '21 at 16:16
  • I copied and pasted the first code you wrote. I didn't understand what the problem was. But now I tried it on a new notebook and the problem solved. Thank you :) – FATMA Jun 18 '21 at 16:48