Lucas Scheepers
Lucas Scheepers

Reputation: 601

Compare two lists of dictionaries in Python. Return non match

I have two lists of dictionaries like below:

incidents = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "REGISTERED"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "REGISTERED"
    }
]

incidents_db = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "PENDING"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "PENDING"
    }
]

I want to compare the incidentId with each other and return the non_match. This is my code now, but it returns just the two items, because it is comparing the whole item instead of only the incidentId. What's a good way to do this?

non_match = []
for i in incidents:
    if i not in incidents_db:
        non_match.append(i)

Upvotes: 0

Views: 499

Answers (4)

ombk
ombk

Reputation: 2111

incidents = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "REGISTERED"
    },
    {
        "incidentId": "RWS03_947565",
        "incidentState": "REGISTERED"
    },
        {
        "incidentId": "RWS02_947565",
        "incidentState": "REGISTERED"
    },
        {
        "incidentId": "RWS05_947565",
        "incidentState": "REGISTERED"
    }
]

incidents_db = [
    {
        "incidentId": "RWS03_949563",
        "incidentState": "PENDING"
    },
    {
        "incidentId": "RWS03_949565",
        "incidentState": "PENDING"
    }
]
for i in incidents:
    c = 0
    for j in incidents_db:
        if j["incidentId"] == i["incidentId"]:
            c+=1
    if c==0:
        incidents_db.append(i)
#output
incidents_db

[{'incidentId': 'RWS03_949563', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_949565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_947565', 'incidentState': 'REGISTERED'},
 {'incidentId': 'RWS02_947565', 'incidentState': 'REGISTERED'},
 {'incidentId': 'RWS05_947565', 'incidentState': 'REGISTERED'}]

for i in incidents:
    c = 0
    for j in incidents_db:
        if j["incidentId"] == i["incidentId"]:
            c+=1
    if c==0:
        i["incidentState"] = "PENDING"
        incidents_db.append(i)
#output

[{'incidentId': 'RWS03_949563', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_949565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS03_947565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS02_947565', 'incidentState': 'PENDING'},
 {'incidentId': 'RWS05_947565', 'incidentState': 'PENDING'}]

Upvotes: 1

Navaneeth Reddy Nalla
Navaneeth Reddy Nalla

Reputation: 349

If you only want to compare two specific items from the dictionary. Do this:

non_match = []

for incident_dict in incidents:
    for db_dict in incidents_db:
        for key, value in incident_dict.items():
            if not db_dict.get(key, False):
                non_match.append(value)

Upvotes: 0

sourab maity
sourab maity

Reputation: 1045

if you want to compare only " incidentId"

non_match = []
for i in range(0,min(len(incidents),len(incidents_db))):
    if incidents[i].get("incidentId") != incidents_db[i].get("incidentId"):
        non_match.append(incidents[i])
print(non_match)

Upvotes: 0

rioV8
rioV8

Reputation: 28673

non_match = []
for a,b in zip(incidents, incidents_db):
    if a.get("incidentId") != b.get("incidentId"):
        non_match.append(a.get("incidentId"))
print(non_match)

Upvotes: 0

Related Questions