Home > Software engineering >  How we can merge dictionaries having same 'id' with the condition to merge 'test_type
How we can merge dictionaries having same 'id' with the condition to merge 'test_type

Time:01-01

How can we merge the dictionary having same 'id' and with condition to merge 'test_type' values?

Input:

[{id:'abc', type:'test', test_type:['404']}, 
 {id:'def', type:'test', test_type:['404','server_error']},
 {id:'abc', type:'test', test_type:['server_error']},
 {id:'abc', type:'test', test_type:['server_error', '404']}]

Expected output:

[{id:'abc', type:'test', test_type:['404','server_error']}, 
 {id:'def', type:'test', test_type:['404','server_error']}]

CodePudding user response:

original_list = [{id:'abc', type:'test', 'test_type':['404']}, 
{id:'def', type:'test', 'test_type':['404','server_error']},
{id:'abc', type:'test', 'test_type':['server_error']},
{id:'abc', type:'test', 'test_type':['server_error', '404']}]
new_list = []
new_dict = {}
for old_dict in original_list:
    if old_dict[id] not in new_dict:
        new_dict[old_dict[id]] = old_dict
    else:
        present_values = new_values = []
        if 'test_type' in new_dict:
            present_values = new_dict[old_dict[id]]['test_type']
        if 'test_type' in old_dict:
            new_values = old_dict['test_type']
        temp_list = list(set(present_values   new_values))
        new_dict[old_dict[id]]['test_type'] = temp_list

final_list = list(new_dict.values())        
print(final_list)

CodePudding user response:

Here is a clean way to do this using itertools.groupby (for creating dictionary groups based on key) and collections.defaultdict (for merging dictionaries).

Please find the explanation as inline comments. Feel free to ask any questions in the comments.

from itertools import groupby
from collections import defaultdict

f = lambda x: x['id'] #condition for sorting & grouping
groups = groupby(sorted(l, key=f), f) #creating grouper object

output = []
for _ , g in groups:
  d = defaultdict(list)
  for i in g:
      d['id'], d['type'] = i['id'], i['type'] #set id and type as same
      d['test_type'].extend(i['test_type'])   #extend list of test_type
  d['test_type'] = list(set(d['test_type']))  #reset test_type as unique values
  output.append(dict(d))
[{'id': 'abc', 'test_type': ['404', 'server_error'], 'type': 'test'},
 {'id': 'def', 'test_type': ['404', 'server_error'], 'type': 'test'}]
  • Related