Python Forum
Comparing items from 2 lists of dictionaries - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Comparing items from 2 lists of dictionaries (/thread-29654.html)



Comparing items from 2 lists of dictionaries - illwill - Sep-14-2020

Hey all,

I working with 2 json data sets, I've imported them into variables so that I now have 2 lists of dictionaries.

I need to check if the items in list A exist in list B. They aren't identical structures though but they do have a few things in common that I can test for, for example each dictionary has a value of name.

I've come up with this bit of code which works but I was wondering if there's a more elegant solution to this.

exists = []
for i in listA:
    for j in listB:
        if (i['name'] == j['name']):
            exists.append(i)
For the dictionaries that already exist the next step is populate another list with those excluded.

Any suggestions would be welcomed.


RE: Comparing items from 2 lists of dictionaries - bowlofred - Sep-14-2020

The dictionaries can have different keys, and if they have the same key they can have different values stored. It's not clear to me which sort of difference you are looking for. What do you want to do if the key doesn't exist? What do you want to do if the key exists but has a different value? What do you want to do if the same key in each has the same value?

For all these cases, you'll probably want to loop over the keys in dictA and examine dictB on each of them to see if it matches. Then store or mark the case that you need.


RE: Comparing items from 2 lists of dictionaries - illwill - Sep-14-2020

(Sep-14-2020, 09:41 PM)bowlofred Wrote: The dictionaries can have different keys, and if they have the same key they can have different values stored. It's not clear to me which sort of difference you are looking for. What do you want to do if the key doesn't exist? What do you want to do if the key exists but has a different value? What do you want to do if the same key in each has the same value?

For all these cases, you'll probably want to loop over the keys in dictA and examine dictB on each of them to see if it matches. Then store or mark the case that you need.

Sorry I need to amend my post. All the dictionaries have a 'name' key not value, so I'm looking to match on the value of that key. Hope that makes more sense now. So for example I might have the following:

new list - ListA[{name:david},{name:michael},{name:todd}]
existing - ListB[{name:david},{name:sarah},{name:james}]

As david already exists I need to either remove him from ListA or populate a new list with him excluded.


RE: Comparing items from 2 lists of dictionaries - bowlofred - Sep-14-2020

Since they're lists, does the order matter? Or are you just looking for the collection of names in one list to the collection of names in the other?

If order matters, you'd use zip().

for dictA, dictB in zip(ListA, ListB):
    if dictA['name'] == dictB['name']:
        # names are the same
        ...
    else
        # names at this position of the list are different
If order didn't matter, I'd make a set() of the names in both lists, then you could easily use set differences to see which names are in one but not the other.


RE: Comparing items from 2 lists of dictionaries - illwill - Sep-14-2020

(Sep-14-2020, 10:16 PM)bowlofred Wrote: Since they're lists, does the order matter? Or are you just looking for the collection of names in one list to the collection of names in the other?

If order matters, you'd use zip().

for dictA, dictB in zip(ListA, ListB):
    if dictA['name'] == dictB['name']:
        # names are the same
        ...
    else
        # names at this position of the list are different
If order didn't matter, I'd make a set() of the names in both lists, then you could easily use set differences to see which names are in one but not the other.

The order doesn't matter. I've mashed together this solution which works but it involves another loop. So it's totally overkill, I know there's a more elegant solution of doing this but my brain is fried at the moment.

for items in exists:
    val = items['name']
    for i in range(len(toUpdate)):
        if toUpdate[i]['name'] == val:
            del toUpdate[i]
            break



RE: Comparing items from 2 lists of dictionaries - bowlofred - Sep-14-2020

listA = [{'name':'david'},{'name':'michael'},{'name':'todd'}]
listB = [{'name':'sarah'},{'name':'david'},{'name':'james'}]


namesA = {x['name'] for x in listA}
namesB = {x['name'] for x in listB}

print ("The following names are in A, but not in B")
print (namesA - namesB)
Output:
The following names are in A, but not in B {'michael', 'todd'}



RE: Comparing items from 2 lists of dictionaries - illwill - Sep-14-2020

(Sep-14-2020, 10:24 PM)bowlofred Wrote:
listA = [{'name':'david'},{'name':'michael'},{'name':'todd'}]
listB = [{'name':'sarah'},{'name':'david'},{'name':'james'}]


namesA = {x['name'] for x in listA}
namesB = {x['name'] for x in listB}

print ("The following names are in A, but not in B")
print (namesA - namesB)
Output:
The following names are in A, but not in B {'michael', 'todd'}

Oh nice, thank you for the introduction to sets. This works great, I just need to figure out how to populate a list (with the whole dictionaries) with the duplicates removed. I'll have a good read about sets. Thanks again!


RE: Comparing items from 2 lists of dictionaries - bowlofred - Sep-14-2020

Perhaps just make setB.

Then iterate over listA. If the name isn't in setB, add it to your new list. If the name is in setB, then ignore it.