Anudocs
Anudocs

Reputation: 686

Combining 2 python lists of dictionaries

I have 2 python list of dictionaries. First python list looks like below :

l1=  [{'compu_method': 'ROTACC', 'aufloesung': '1024'}, 
{'compu_method': 'TMPOUTS', 'aufloesung': '1'}]

Second python list looks like this:

l2=  [{'signal_name': 'XXXX', 'compu_method': 'ROTACC', 'min_wert': '-500', 'max_wert': '500'},
 {'signal_name': 'YYYY', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184'}, 
 {'signal_name': 'ZZZZ', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184'}]

Both lists have compu_method common. I want aufloesung from first list for all the compu_method which exists in second list. I would like to have a final list like this :

[{'signal_name': 'XXXX', 'compu_method': 'ROTACC', 'min_wert': '-500', 'max_wert': '500','aufloesung': '1024'},
 {'signal_name': 'YYYY', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184','aufloesung': '1'}, 
 {'signal_name': 'ZZZZ', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184','aufloesung': '1'}]

I tried below code but it doesnt give the desired output:

from collections import defaultdict
d = defaultdict(dict)
for l in (l1, l2):
    for elem in l:
        d[elem['compu_method']].update(elem)

l3 = d.values()
print(l3)

What modification is needed in the code?

Upvotes: 0

Views: 65

Answers (4)

sentence
sentence

Reputation: 8913

Another solution could be:

import copy

l3 = copy.deepcopy(l2)

[ll3.update({'aufloesung':ll1['aufloesung']}) for ll3 in l3 for ll1 in l1 if ll3['compu_method']==ll1['compu_method']]

and you get:

l3

[{'signal_name': 'XXXX',
  'compu_method': 'ROTACC',
  'min_wert': '-500',
  'max_wert': '500',
  'aufloesung': '1024'},
 {'signal_name': 'YYYY',
  'compu_method': 'TMPOUTS',
  'min_wert': '-70',
  'max_wert': '184',
  'aufloesung': '1'},
 {'signal_name': 'ZZZZ',
  'compu_method': 'TMPOUTS',
  'min_wert': '-70',
  'max_wert': '184',
  'aufloesung': '1'}]

Upvotes: 0

RoadRunner
RoadRunner

Reputation: 26315

You could first transform l1 to be a dictionary:

l1_map = {x["compu_method"]: x["aufloesung"] for x in l1}

Then simply update l2(or rebuild a new list):

for d in l2:
    d["aufloesung"] = l1_map[d["compu_method"]]

print(l2)
# [{'signal_name': 'XXXX', 'compu_method': 'ROTACC', 'min_wert': '-500', 'max_wert': '500', 'aufloesung': '1024'}, {'signal_name': 'YYYY', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184', 'aufloesung': '1'}, {'signal_name': 'ZZZZ', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184', 'aufloesung': '1'}]

Or in terser ** syntax:

[{**d, "aufloesung": l1_map[d["compu_method"]]} for d in l2]

This assumes that "compu_method" will be present in the transformed dictionary. If you believe a KeyError could arise, then using dict.get() as shown in @ssm's answer will be safer.

Upvotes: 1

ssm
ssm

Reputation: 5373

You create a dictionary:

d1 = { m['compu_method']: m['aufloesung'] for m in l1} 

Once you have that, its just a matter of mapping into the new one:

for i, m in enumerate(l2):
    l2[i]['aufloesung'] = d1.get(m['compu_method'], None)

Should do the trick ...

Upvotes: 5

Arkistarvh Kltzuonstev
Arkistarvh Kltzuonstev

Reputation: 6920

Without using other modules, you can try this :

for i in l2:
    for j in l1:
        if i['compu_method']==j['compu_method']:
            i['aufloesung']=j['aufloesung']

OUTPUT :

[{'signal_name': 'XXXX', 'compu_method': 'ROTACC', 'min_wert': '-500', 'max_wert': '500', 'aufloesung': '1024'}, 
{'signal_name': 'YYYY', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184', 'aufloesung': '1'}, 
{'signal_name': 'ZZZZ', 'compu_method': 'TMPOUTS', 'min_wert': '-70', 'max_wert': '184', 'aufloesung': '1'}]

Upvotes: 2

Related Questions