I'm working with python
dictionaries and I want to combine it. The dicts are like below,
devices = [{'EUI':123,'Name':'gum 1'},{'EUI':456,'Name':'gum 2'},{'EUI':789,'Name':'gum 3'}]
data = [{'EUI':123,'data':111},{'EUI':456,'data':222},{'EUI':789,'data':333},{'EUI':456,'data':444},{'EUI':789,'data':555}]
They have in common the EUI
(an identifier). What I'm doing is using a pair of loops and check if the EUI
is the same or not. If is the same, I combine both dicts.
The final result is,
[{'EUI': 123, 'data': 111, 'Name': 'gum 1'}, {'EUI': 456, 'data': 222, 'Name': 'gum 2'}, {'EUI': 789, 'data': 333, 'Name': 'gum 3'}, {'EUI': 456, 'data': 444, 'Name': 'gum 2'}, {'EUI': 789, 'data': 555, 'Name': 'gum 3'}]
My full code is the following,
devices = [{'EUI':123,'Name':'gum 1'},{'EUI':456,'Name':'gum 2'},{'EUI':789,'Name':'gum 3'}]
data = [{'EUI':123,'data':111},{'EUI':456,'data':222},{'EUI':789,'data':333},{'EUI':456,'data':444},{'EUI':789,'data':555}]
print(data)
for da in data:
for dev in devices:
if dev['EUI'] == da['EUI']:
da.update(dev)
break
print(data)
Actually it works perfectly, but I think it could a better/easier/pythonic option to do the same. Somebody knows another way to do it?
Thank you very much!