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


Messages In This Thread
Comparing items from 2 lists of dictionaries - by illwill - Sep-14-2020, 08:38 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  comparing 2 lists rwahdan 2 1,624 Jul-18-2021, 06:30 PM
Last Post: DeaD_EyE
  Split dict of lists into smaller dicts of lists. pcs3rd 3 2,383 Sep-19-2020, 09:12 AM
Last Post: ibreeden
  Comparing Items Different Data Frames With a WHILE Loop JoeDainton123 1 1,954 Jul-30-2020, 04:11 AM
Last Post: scidam
  Searching through Nested Dictionaries and Lists Dave_London 1 7,713 Jul-09-2020, 03:36 PM
Last Post: mrdominikku
  How to put the items of one list in new generated lists Bobbear 1 1,660 Jun-12-2020, 06:08 AM
Last Post: buran
  Compare Two Lists and Replace Items In a List by Index nagymusic 2 2,893 May-10-2020, 05:28 AM
Last Post: deanhystad
  Add items from one list to list of lists PUP280 5 2,973 May-05-2020, 03:47 PM
Last Post: PUP280
  putting 2 lists items side by side Jiwan 1 2,245 Apr-30-2020, 01:04 AM
Last Post: Larz60+
  Finding value in nested dictionaries with lists mart79 16 8,098 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 2,777 Feb-21-2020, 02:40 PM
Last Post: Cosmosso

Forum Jump:

User Panel Messages

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