Search code examples
pythondictionarykeyduplicatesdefaultdict

List of values for duplicate keys in dictionary Python


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.


Solution

  • 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')]