Home > Net >  Merging list with nested dictonary and dictonary in python
Merging list with nested dictonary and dictonary in python

Time:12-16

dict1 = {'cost1': {'A': '22.727549567', 'B': '22.4940797544', 'C': '19.2819629649', 'D': '24.9302268902', 'E': '28.7951523193', 'F': '17.0993721357', 'G': '322.3279015934'}, 'cost2': {'A': 1511.7474897264, 'B': 2234.0384991703, 'C': 1811.299631493, 'D': 1394.9521800232997, 'E': 2717.4205026055, 'F': 2853.2642152811995, 'G': 63.0732507946}}

list1=[{'name': 'C','owner': 'System', 'status': 'ACTIVE'}, {'name': 'B', 'createdAt': '342 days 18 hours ago', 'owner': 'System', 'status': 'ACTIVE'}, {'name': 'C', 'createdAt': '205 days 4 hours ago', 'owner': 'ps', 'status': 'ACTIVE'}, {'name': 'A', 'createdAt': '342 days 17 hours ago', 'owner': 'System', 'status': 'ACTIVE'}, {'name': 'G', 'createdAt': '279 days 5 hours ago', 'owner': 'sb', 'status': 'ACTIVE'}, {'name': 'E', 'createdAt': '167 days 19 hours ago', 'owner': 'uk', 'status': 'ACTIVE'}, {'name': 'D', 'createdAt': '2 hours 2 mins ago', 'owner': 'uk', 'status': 'ACTIVE'}, {'name': 'F', 'createdAt': '1 days 10 hours ago', 'owner': 'chu', 'status': 'ACTIVE'}]

Result=[{'name': 'C','owner': 'System', 'status': 'ACTIVE'}, {'name': 'B', 'createdAt': '342 days 18 hours ago', 'owner': 'System', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'C', 'createdAt': '205 days 4 hours ago', 'owner': 'ps', 'status': 'ACTIVE','cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'A', 'createdAt': '342 days 17 hours ago', 'owner': 'System', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'G', 'createdAt': '279 days 5 hours ago', 'owner': 'sb', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'E', 'createdAt': '167 days 19 hours ago', 'owner': 'uk', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'D', 'createdAt': '2 hours 2 mins ago', 'owner': 'uk', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}, {'name': 'F', 'createdAt': '1 days 10 hours ago', 'owner': 'chu', 'status': 'ACTIVE', 'cost_1': <value from dict1> ,'cost_2': < value from dict 1>}]

Want to merge cost1 and cost2 per from dict1 into list1 as shown in result.

CodePudding user response:

A single comprehension does the trick:

result = [
    d if 'createdAt' not in d
    else d | {k: sub[d['name']] for k, sub in dict1.items()}
    for d in list1
]
print(result)

Result:

[{'name': 'C', 'owner': 'System', 'status': 'ACTIVE'}, {'name': 'B', 'createdAt': '342 days 18 hours ago', 'owner': 'System', 'status': 'ACTIVE', 'cost1': '22.4940797544', 'cost2': 2234.0384991703}, {'name': 'C', 'createdAt': '205 days 4 hours ago', 'owner': 'ps', 'status': 'ACTIVE', 'cost1': '19.2819629649', 'cost2': 1811.299631493}, {'name': 'A', 'createdAt': '342 days 17 hours ago', 'owner': 'System', 'status': 'ACTIVE', 'cost1': '22.727549567', 'cost2': 1511.7474897264}, {'name': 'G', 'createdAt': '279 days 5 hours ago', 'owner': 'sb', 'status': 'ACTIVE', 'cost1': '322.3279015934', 'cost2': 63.0732507946}, {'name': 'E', 'createdAt': '167 days 19 hours ago', 'owner': 'uk', 'status': 'ACTIVE', 'cost1': '28.7951523193', 'cost2': 2717.4205026055}, {'name': 'D', 'createdAt': '2 hours 2 mins ago', 'owner': 'uk', 'status': 'ACTIVE', 'cost1': '24.9302268902', 'cost2': 1394.9521800232997}, {'name': 'F', 'createdAt': '1 days 10 hours ago', 'owner': 'chu', 'status': 'ACTIVE', 'cost1': '17.0993721357', 'cost2': 2853.2642152811995}]

Explanation:

  • result is a list of dictionaries
  • if the dictionary does not contain a 'createdAt' key, it should just be included unchanged
  • the the dictionary does have that key, it should be updated with the keys and selected values from dict1, the '|' symbol updates one dictionary with another
  • those values are selected using the name from the dictionary being processed, i.e. d['name']
  • all the dictionaries from the original list1 are processed one at a time
  • Related