Home > Enterprise >  how to convert list of dict to dict when some keys repeated
how to convert list of dict to dict when some keys repeated

Time:09-07

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 ?

CodePudding user response:

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'}]}

CodePudding user response:

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'}]}
           )
  • Related