Python Forum
Comparing items from 2 lists of dictionaries
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Comparing items from 2 lists of dictionaries
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(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
Reply
#6
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'}
Reply
#7
(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!
Reply
#8
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing 2 lists rwahdan 2 360 Jul-18-2021, 06:30 PM
Last Post: DeaD_EyE
  Comparing Items Different Data Frames With a WHILE Loop JoeDainton123 1 575 Jul-30-2020, 04:11 AM
Last Post: scidam
  Searching through Nested Dictionaries and Lists Dave_London 1 676 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  How to put the items of one list in new generated lists Bobbear 1 677 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 1,279 May-10-2020, 05:28 AM
Last Post: deanhystad
  Add items from one list to list of lists PUP280 5 1,412 May-05-2020, 03:47 PM
Last Post: PUP280
  putting 2 lists items side by side Jiwan 1 778 Apr-30-2020, 01:04 AM
Last Post: Larz60+
  Finding value in nested dictionaries with lists mart79 16 2,669 Mar-08-2020, 08:16 PM
Last Post: ndc85430
  Can't seem to figure out how to put all of the lists items from a loop into 1 list Cosmosso 4 1,092 Feb-21-2020, 02:40 PM
Last Post: Cosmosso
  Nested Dictionaries with Lists BriHaug 1 1,298 Feb-23-2019, 02:10 AM
Last Post: ichabod801

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020