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

Question:

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 ?

Asked By: Reut

||

Answers:

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'}]}
Answered By: Chris

You can use dict.setdefault Or collections.defaultdict.

from collections import defaultdict

results = [{'year': 2020, 'id': '321abc', 'color': 'blue'},
           {'year': 2020, 'id': '412nbg', 'color': 'brown'},
           {'year': 2021, 'id': 'klp54', 'color': 'yellow'}]
res = {}
res_2 = defaultdict(list)
for dct in results:
    res.setdefault(dct['year'], []).append({
        'id' : dct['id'], 
        'color':dct['color']
    })

    # Or
    res_2[dct['year']].append({'id' : dct['id'], 'color':dct['color']})

    
print(res)
print(res_2)

Output:

# res =>
{
    2020: [{'id': '321abc', 'color': 'blue'}, 
           {'id': '412nbg', 'color': 'brown'}], 
    2021: [{'id': 'klp54', 'color': 'yellow'}]
}


# res_2 =>
defaultdict(<class 'list'>, 
            {2020: [{'id': '321abc', 'color': 'blue'}, 
                    {'id': '412nbg', 'color': 'brown'}], 
             2021: [{'id': 'klp54', 'color': 'yellow'}]}
           )
Answered By: I'mahdi
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.