anu010291
anu010291

Reputation: 85

Merge list of dictionary based on the key values

I have two list of dictionaries, where i have to merge the two lists based on key value.

a = [{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344}]

b = [{'mac': 'e200383d1149a4c975a59618', 'status': 'location_recording'}, {'mac': 'e200383d1149a90975a59629', 'status': 'location_environment'}]


expected output = [
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444, 'status': 'location_recording'}}, 
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5, 'status': 'location_environment'}, 
{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344, 'status': 'location_environment'}]

Can someone help me out with the expected output?

Upvotes: 0

Views: 52

Answers (3)

Shadowfax
Shadowfax

Reputation: 586

you can use simple nested loop as follows:

for item in b:
    for site in a:
        if site.get('mac') == item.get('mac'):
            site.update({'status':item.get('status')})

Output

[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]

Upvotes: 1

Rakesh
Rakesh

Reputation: 82765

One approach is to use a loopup dict.

Ex:

a = [{'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a4c975a59618', 'rssi': -63.116279069767444}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_1', 'mac': 'e200383d1149a90975a59629', 'rssi': -61.5}, {'site': 'KRM Plaza South Tower', 'building': 'd-block', 'level': 'reception', 'gw_mac': 'b827eb36fb0b_2', 'mac': 'e200383d1149a90975a59629', 'rssi': -59.086021505376344}]
b = [{'mac': 'e200383d1149a4c975a59618', 'status': 'location_recording'}, {'mac': 'e200383d1149a90975a59629', 'status': 'location_environment'}]

b = {i['mac']: i["status"] for i in b}  #loopup dict

for i in a:
    if i["mac"] in b:
        i.update({"status": b[i["mac"]]})
print(a)

Output:

[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]

Upvotes: 1

Kamehameha
Kamehameha

Reputation: 5473

You can iterate through both of the lists and merge the dicts if the "mac" key matches.

Here's a single-line solution using list comprehension(assuming the lists are a & b like in the question)

In [6]: [{**a_val, **b_val} for a_val in a for b_val in b if b_val["mac"] == a_val["mac"]]
Out[6]:
[{'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a4c975a59618',
  'rssi': -63.116279069767444,
  'site': 'KRM Plaza South Tower',
  'status': 'location_recording'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_1',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -61.5,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'},
 {'building': 'd-block',
  'gw_mac': 'b827eb36fb0b_2',
  'level': 'reception',
  'mac': 'e200383d1149a90975a59629',
  'rssi': -59.086021505376344,
  'site': 'KRM Plaza South Tower',
  'status': 'location_environment'}]

NOTE: You can perform the dictionary merge using the ** above Python 3.5, or use the dictionary's copy and update function for versions below it.

Upvotes: 1

Related Questions