Karthikeyan KR
Karthikeyan KR

Reputation: 1174

Efficiently Update List of dicts by comparison

I have two list of dicts,

Say,

required = [
  {"name": "a", "req_Key": "a"},
  {"name": "b", "req_Key": "b"},
  {"name": "c", "req_Key": "c"},
  {"name": "d", "req_Key": "d"}
]

updated = [
  {"name": "a", "output_id": "Oa"},
  {"name": "d", "output_id": "Od"}
]

Now I need to update required dicts by comparing with updated dicts,

So the output will look like

[
  {'req_Key': 'a', 'name': 'a', 'output_id': 'Oa'},
  {'req_Key': 'b', 'name': 'b'},
  {'req_Key': 'c', 'name': 'c'},
  {'req_Key': 'd', 'name': 'd', 'output_id': 'Od'}
]

I am using loops to do this, but how can I do this efficiently by avoiding loops?

Sample Code:

required = [{"name":"a","req_Key":"a"},{"name":"b","req_Key":"b"},{"name":"c","req_Key":"c"},{"name":"d","req_Key":"d"}]

updated = [{"name":"a","output_id":"Oa"},{"name":"d","output_id":"Od"}]

for updated_record in updated:

    for req_record in required:
        if updated_record["name"] == req_record["name"]:
            req_record.update(updated_record)
            print(req_record)

Its working, but I need better and efficient method.

Upvotes: 0

Views: 51

Answers (2)

user2390182
user2390182

Reputation: 73450

Transform your update data first, so you can get constant lookup times:

update_data = {d['name']: d for d in update}

for d in required:
    d.update(update_data.get(d['name'], {}))

This assumes that name is unique in your update data. Otherwise, you can do:

from collections import defaultdict

update_data = defaultdict(dict)
for d in update:
    update_data[d['name']].update(d)

for d in required:
    d.update(update_data.get(d['name'], {}))

These approaches both iterate each list only once (O(N+M)) which is a significant improvement over the nested lists approach (O(M*N)).

Upvotes: 1

Rakesh
Rakesh

Reputation: 82755

This is one approach.

required = [{"name":"a","req_Key":"a"},{"name":"b","req_Key":"b"},{"name":"c","req_Key":"c"},{"name":"d","req_Key":"d"}]
updated = [{"name":"a","output_id":"Oa"},{"name":"d","output_id":"Od"}]

updated = dict((i["name"], i["output_id"]) for i in updated)

for i in required:
    i.update({'output_id': updated.get(i["name"])})
print(required)

Output:

[{'output_id': 'Oa', 'req_Key': 'a', 'name': 'a'}, {'output_id': None, 'req_Key': 'b', 'name': 'b'}, {'output_id': None, 'req_Key': 'c', 'name': 'c'}, {'output_id': 'Od', 'req_Key': 'd', 'name': 'd'}]

Upvotes: 0

Related Questions