Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to convert list of dict to dict when some keys repeated

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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 ?

>Solution :

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'}]}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading