Merging a list of dicts into one dict [duplicate]

I have a list of dicts which are guaranteed to have the same keys. Each element of a dict is a list. What’s the most elegant way to merge these dicts into one dict extending the lists of the same keys?

I have:

[
    {
        'x': ['a', 'b'],
        'y': ['c', 'd']
    },
    {
        'x': ['e', 'f'],
        'y': ['g', 'h']
    },
]

I want:

{
    'x': ['a', 'b', 'e', 'f'],
    'y': ['c', 'd', 'g', 'h']
}

Leave a Comment