Creating a dict from list of key, value tuples while maintaining duplicate keys

Viewed 19543

So I've got a comprehension to the effect of:

dict((x.key, x.value) for x in y)

The problem, of course, is that if there's multiple x.keys with the same value, they get collapsed with the last x.value with that particular x.key as the only surviving member. I want to actually make the values of the resulting dict a list:

{
    'key1': ['value1'],
    'key2': ['value2', 'value3', 'value4'],
    'key3': ['value5'],
    # etc.
}

Is this logic possible with a comprehension?

3 Answers
Related