List of values for duplicate keys in dictionary Python

17,066

Solution 1

If I understand correctly your attetion, you are trying to merge various dictionaries. One way using built-ins (I am sure that soon someone will give you a numpy and collections answer) could look like this:

ds = [
    {'a':10, 'b': 0, 'c': 2},
    {'a':7, 'b': 4, 'c': 4},
    {'a':4, 'b': 5, 'c': 3} ]

merged = {}
for d in ds:
    for k, v in d.items ():
        if k not in merged: merged [k] = []
        merged [k].append (v)

print (merged)

(Quite verbose for clarity)

EDIT: After having read your comment stating "The result I want is a list of values/key.", you can use this on the resulting merged dictionary:

print ( [ (v, k) for k, v in merged.items () ] )

This yields:

[([10, 7, 4], 'a'), ([2, 4, 3], 'c'), ([0, 4, 5], 'b')]

Solution 2

Is this what you need?

in_dicts = [{'a':10, 'b': 0, 'c': 2}, {'a':7, 'b': 4, 'c': 4}, {'a':4, 'b': 5, 'c': 3}]
out_dict = {}

for in_d in in_dicts:
  for k, v in in_d.iteritems():
    out_dict.setdefault(k, []).append(v)  
print out_dict

{'a': [10, 7, 4], 'c': [2, 4, 3], 'b': [0, 4, 5]}

Share:
17,066
askance
Author by

askance

Updated on June 04, 2022

Comments

  • askance
    askance almost 2 years

    Apologies in advance if this question has already been explored here - I looked at different answers here but couldn't find what I need.

    My goal is to create a dictionary like this -- {'a':[10, 9, 10, 10], 'b':[10, 9, 1, 0], 'c':[0, 5, 0, 1], and so on}

    What I have is multiple dictionaries with duplicate keys (same keys in every other dictionary), something like this {'a':10, 'b': 0, 'c': 2} {'a':7, 'b': 4, 'c': 4} {'a':4, 'b': 5, 'c': 3}

    I have no way of knowing the number of such dictionaries, or if there are keys continuing up to 'f', or a 'g' in them but I know that the keys are duplicated. I've tried defaultdict but what I get is--

    defaultdict(<type 'list'>, {'a': [10]})
    defaultdict(<type 'list'>, {'a': [10], 'b': [3]})
    defaultdict(<type 'list'>, {'a': [10], 'b': [3], 'c': [0]})
    

    and then the same thing for the next dictionary --

    defaultdict(<type 'list'>, {'a': [4]})
    defaultdict(<type 'list'>, {'a': [4], 'b': [5]})
    defaultdict(<type 'list'>, {'a': [4], 'b': [5], 'c': [1]})
    

    The code I have for the above output is --

    d = collections.defaultdict(list)
        for k, v in z.iteritems():
            d[k].append(v)
            c = d.items()
            print d
    

    If I do a print c instead (to print the d.items()) I get --

    [('a', [10])]
    [('a', [10]), ('b', [3])]
    [('a', [10]), ('c', [0]), ('b', [3])]
    

    which is again repeated for each dictionary. How do I get 1 dict holding all the keys, values --

    {'a':[10,0,..], 'b':[4, 3, 4,..], etc.} ?
    

    I should also add that the dicts I have are the result of a for loop and not stored individually in a unique variable.