3

I have nested dictionaries in a list of dictionaries, I want to merge the lists based on 'id'

res = [{'i': ['1'], 'id': '123'},
       {'i': ['1'], 'id': '123'},
       {'i': ['1','2','3','4','5','6'],'id': '123'},
       {'i': ['1'], 'id': '234'},
       {'i': ['1','2','3','4','5'],'id': '234'}]

Desired output:

[{'i': [1, 1, 1, 2, 3, 4, 5, 6], 'id': '123'},
 {'i': [1, 1, 2, 3, 4, 5], 'id': '234'}]

I am trying to merge the nested dictionaries based on key "id". I couldn't figure out the best way out:

import collections
d = collections.defaultdict(list)
for i in res:
    for k, v in i.items():
        d[k].extend(v)

The above code is merging all the lists, but i wantto merge lists based on key "id".

Mad Physicist
  • 107,652
  • 25
  • 181
  • 264
louis
  • 595
  • 3
  • 9
  • 24

3 Answers3

3

Something like this should do the trick

from collections import defaultdict

merged = defaultdict(list)
for r in res:
    merged[r['id']].extend(r['i'])

output = [{'id': key, 'i': merged_list} for key, merged_list in merged.items()]
michaeldel
  • 2,204
  • 1
  • 13
  • 19
1

The following produces the desired output, using itertools.groupby:

from operator import itemgetter
from itertools import groupby

k = itemgetter('id')

[
    {'id': k, 'i': [x for d in g for x in d['i']]} 
    for k, g in groupby(sorted(res, key=k), key=k)
]
user2390182
  • 72,016
  • 6
  • 67
  • 89
0

I'm not sure what the expected behavior should be when there are duplicates -- for example, should the lists be:

  • treated like a set() ?
  • appended, and there could be multiple items, such as [1,1,2,3...] ?
  • doesn't matter -- just take any

Here would be one variation where we use a dict comprehension:

{item['id']: item for item in res}.values()
# [{'i': ['1', '2', '3', '4', '5'], 'id': '234'}, {'i': ['1', '2', '3', '4', '5', '6'], 'id': '123'}]

If you provide a bit more information in your question, I can update the answer accordingly.

samuelbrody1249
  • 4,379
  • 1
  • 15
  • 58