Bryan
Bryan

Reputation: 17703

Update list of dictionaries based on another list of dictionaries

I have two lists of dictionaries, one is a list of project identifiers and the other a list of completed project identifiers. I'm looking to add a key to the project identifier list, based on existence in the completed list.

Current code

>>> projects = [{'id': 1}, {'id': 2}, {'id': 3}]
>>> completes = [{'id': 1}, {'id': 2}]
>>> for complete in completes:
...     for project in projects:
...         if project["id"] == complete["id"]:
...             project["complete"] = 1
...         else:
...             project["complete"] = 0
...
>>> print projects
[{'id': 1, 'complete': 0}, {'id': 2, 'complete': 1}, {'id': 3, 'complete': 0}]

Expected output

[{'id': 1, 'complete': 1}, {'id': 2, 'complete': 1}, {'id': 3, 'complete': 0}]

How can I break out of the nested loop once a project has been flagged as complete? Is there another approach I should consider instead of working with the nested loop?

Upvotes: 0

Views: 120

Answers (3)

trevorj
trevorj

Reputation: 90

If you store them as a dict of dicts keyed by id versus an array of dicts, it becomes much easier (in my opinion):

projects = [{'id': 1}, {'id': 2}, {'id': 3}]
completes = [{'id': 1}, {'id': 2}]

projects_dict = {}
for p in projects:
    projects_dict[p['id']] = p

completes_dict = {}
for c in completes:
    completes_dict[c['id']] = c

for k in projects_dict.keys():
    projects_dict[k]['complete'] = int(k in completes_dict)

Of course instead of looping through completes, just add the 'complete' key wherever it is you create the 'completes' array of dicts.

Upvotes: 1

William
William

Reputation: 3034

I can't comment, but what jimhark is saying is good, except I think you need to say something like

for project in projects:
    project['complete'] = project['id'] in (complete['id'] for complete in completes)

Edit: He has since changed his answer to be correct

Upvotes: 1

jimhark
jimhark

Reputation: 5046

Edit - fixed (thanks to @sotapme)

Why not something like:

cids = [c['id'] for c in completes]
for project in projects:
    project["complete"] = project["id"] in cids

This will set project["complete"] to True or False, which I suggest is better. If you really need 1 and 0, then:

    project["complete"] = int(project["id"] in cids)

Upvotes: 3

Related Questions