I have list of dictionaries, similar to this:
results=[{'year':2020,'id':'321abc','color':'blue'},
{'year':2020,'id':'412nbg','color':'brown'},
{'year':2021,'id':'klp54','color':'yellow'}...]
I want to organize it, to be one dictionary instead of list with dictionary, and also, to organize it so I have year as key, and then all the id and colors as values. I saw this post which has similar problem, however, they keys there were unique (names in the referenced post), while I have it repeating (years in my example).
So in the end maybe it will be nested dictionary , something like this:
results={ 2020:{id{},color{}},2020:{id{},color{},2022:{id:{},color:{}}
(when I have many ids and colors in each year) How can I do this ?
With itertools.groupby
and a few list and dictionary comprehensions, this is trivial. Remember that you need to sort first or groupby
will not work the way you want. You will get repeated groups.
results = [{'year': 2020, 'id': '321abc', 'color': 'blue'},
{'year': 2020, 'id': '412nbg', 'color': 'brown'},
{'year': 2021, 'id': 'klp54', 'color': 'yellow'}]
from itertools import groupby
from operator import itemgetter
year = itemgetter('year')
r = sorted(results, key=year)
# [{'year': 2020, 'id': '321abc', 'color': 'blue'},
# {'year': 2020, 'id': '412nbg', 'color': 'brown'},
# {'year': 2021, 'id': 'klp54', 'color': 'yellow'}]
g = groupby(r, key=year)
# <itertools.groupby object at 0x7f9f2a232138>
{k: [{'id': x['id'], 'color': x['color']} for x in v]
for k, v in g}
# {2020: [{'id': '321abc', 'color': 'blue'},
# {'id': '412nbg', 'color': 'brown'}],
# 2021: [{'id': 'klp54', 'color': 'yellow'}]}