Merging two or more dictionaries when they have the same key value pairs

RH-NewDeveloper

I am trying to merge two or more dictionaries in a list to combine them using same set of key value pairs. If the specified key value pairs exists, then merge the other keys for those dictionaries gets added under 'other_cols'. Below is what my input looks like and what I am expecting as an output.

input_list = [{'a': 1, 'b' : 2, 'c': 3, 'd': 4},
              {'a': 1, 'b' : 2, 'c': 5, 'd': 6},
              {'a': 9, 'b' : 10, 'c': 11, 'd': 12},
              {'a': 9, 'b' : 10, 'c': 13, 'd': 14},
              {'a': 9, 'b' : 10, 'c': 15, 'd': 16},
              {'a': 17, 'b' : 18, 'c': 19, 'd': 20},
              {'a': 1, 'b' : 2, 'c': 7, 'd': 8}]

merge_by_keys = ['a','b']

expected_output_list = [{'a': 1, 'b' : 2, 'other_cols':[{'c': 3, 'd': 4},
                                                        {'c': 5, 'd': 6},
                                                        {'c': 7, 'd': 8}],
                        {'a': 9, 'b' : 10, 'other_cols':[{'c': 11, 'd': 12},
                                                         {'c': 13, 'd': 14},
                                                         {'c': 15, 'd': 16}],
                        {'a': 17, 'b' : 18, 'other_cols':[{'c': 19, 'd': 20}]}

Your help is greatly appreciated. :)

Alain T.

here's one way to do it using a dictionary to group entries and turning its values into a list at the end.

input_list = [{'a': 1, 'b' : 2, 'c': 3, 'd': 4},
              {'a': 1, 'b' : 2, 'c': 5, 'd': 6},
              {'a': 9, 'b' : 10, 'c': 11, 'd': 12},
              {'a': 9, 'b' : 10, 'c': 13, 'd': 14},
              {'a': 9, 'b' : 10, 'c': 15, 'd': 16},
              {'a': 17, 'b' : 18, 'c': 19, 'd': 20},
              {'a': 1, 'b' : 2, 'c': 7, 'd': 8}]

merge_keys = ['a','b']

grouped = dict()
for d in input_list:
    groupKey  = tuple(map(d.get,merge_keys))
    groupDict = grouped.setdefault(groupKey,{k:d.pop(k) for k in merge_keys})
    groupDict.setdefault('other_cols',[]).append(d)
result = list(grouped.values())

print(result)
[{'a': 1,  'b': 2,  'other_cols': [{'c': 3, 'd': 4},
                                   {'c': 5, 'd': 6},
                                   {'c': 7, 'd': 8}]},
 {'a': 9,  'b': 10, 'other_cols': [{'c': 11, 'd': 12},
                                   {'c': 13, 'd': 14},
                                   {'c': 15, 'd': 16}]},
 {'a': 17, 'b': 18, 'other_cols': [{'c': 19, 'd': 20}]}]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Merging Two Dictionaries Values If They have The Same Keys

Merging two Python dictionaries in a dictionary having the same key

Merge two pairs in the same array if the pairs have the same value

Loop for merging dictionaries with the same key

Merging two list of dictionaries according to a key value in Ansible

Merge two python dictionaries on the same key and value

Merge Two Dictionaries that Share Same Key:Value

Comparing two dictionaries and checking how many (key, value) pairs are equal

Find matching key-value pairs of two dictionaries

Combining Key-Value Pairs from two Dictionaries

How can I merge two dictionaries with multiple key value pairs

how to output an objects keys when some key/value pairs have different keys but same values

How to sum elements in list of dictionaries if two or more key values are the same

Have two or more tuples with the same attribute value

How to combine values in two different dictionaries that have the same key in python

Merging rows from two files if they have the same column value

Is it possible to order JSON object of Key Value Pairs when keys change and values are more key value pairs.

Merging two list of dictionaries based on key

Merge two arrays that both have key value pairs (Ruby)

Compare the keys of two dictionaries and create a dictionary with key, value pairs that does not match the key in the other dictionary - Python

Compare dictionaries and delete key:value pairs

Flatten list of dictionaries with multiple key, value pairs

Creating a list of dictionaries with numerous key value pairs

Returning key-value pairs in a list of dictionaries

Finding dictionaries which don't have forbidden key-value pairs

Python sum values of list of dictionaries if two other key value pairs match

Get dictionaries in a list where the key:value pairs are in another list of dictionaries

Python Dictionaries: Grouping Key, Value pairs based on a common key, value

Merging two dictionaries in Python with a key consisting of two values