Best way to combine a list of dicts into one dict in python?

Question:

I’ve been working with a long list of dicts for a while and can’t seem to wrap my head around how to sort it the way I’d like. Here’s a shorter example:

artist_movement = [
{'movement': 'Abstract Expressionism', 'artist': 'William Baziotes'}, 
{'movement': 'Modern Art', 'artist': 'Alexander Calder'}, 
{'movement': 'Abstract Expressionism', 'artist': 'Grace Hartigan'},
{'movement': 'Cubism', 'artist': 'Pablo Picasso'}, 
{'movement': 'Cubism', 'artist': 'Peter Blume'}, 
{'movement': 'Abstract Expressionism', 'artist': 'Norman Wilfred Lewis'},
{'movement': 'Modern Art', 'artist': 'Lucian Freud'}
]

I’d like to sort the artists by movement in in a similar way to this:

artist_by_movement = [
{'Abstract Expressionism':['William Baziotes', 'Grace Hartigan', 'Norman Wilfred Lewis']},
{'Modern Art':['Alexander Calder', 'Lucian Freud']},
{'Cubism':['Peter Blume', 'Pablo Picasso']}
]

Thanks for any help!

Asked By: djangonoob

||

Answers:

You can use defaultdict to create a dict whose keys are the movements and values are a list of artists in that movement. All defaultdict does is auto-create a list when a new key is seen.

import collections

artist_movement = [
{'movement': 'Abstract Expressionism', 'artist': 'William Baziotes'}, 
{'movement': 'Modern Art', 'artist': 'Alexander Calder'}, 
{'movement': 'Abstract Expressionism', 'artist': 'Grace Hartigan'},
{'movement': 'Cubism', 'artist': 'Pablo Picasso'}, 
{'movement': 'Cubism', 'artist': 'Peter Blume'}, 
{'movement': 'Abstract Expressionism', 'artist': 'Norman Wilfred Lewis'},
{'movement': 'Modern Art', 'artist': 'Lucian Freud'}
]

artist_by_movement = collections.defaultdict(list)
for d in artist_movement:
    artist_by_movement[d['movement']].append(d['artist'])

If you want a more traditional index to the original dictionaries (perhaps there is more interesting information in them) you could do

artist_by_movement = collections.defaultdict(list)
for d in artist_movement:
    artist_by_movement[d['movement']].append(d)
Answered By: tdelaney
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.