Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1011843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:51:16+00:00 2026-05-16T09:51:16+00:00

I have two Python lists of dictionaries, entries9 and entries10 . I want to

  • 0

I have two Python lists of dictionaries, entries9 and entries10. I want to compare the items and write joint items to a new list called joint_items. I also want to save the unmatched items to two new lists, unmatched_items_9 and unmatched_items_10.

This is my code. Getting the joint_items and unmatched_items_9 (in the outer list) is quite easy: but how do I get unmatched_items_10 (in the inner list)?

for counter, entry1 in enumerate(entries9):
    match_found = False
    for counter2,entry2 in enumerate(entries10):
        if match_found:
            continue
        if entry1[a]==entry2[a] and entry1[b]==entry2[b]: # the dictionaries only have some keys in common, but we care about a and b
            match_found = True
            joint_item = entry1
            joint_items.append(joint_item)
            #entries10.remove(entry2) # Tried this originally, but realised it messes with the original list object!
    if match_found:
        continue
    else: 
        unmatched_items_9.append(entry1)

Performance is not really an issue, since it’s a one-off script.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T09:51:17+00:00Added an answer on May 16, 2026 at 9:51 am

    The equivalent of what you’re currently doing, but the other way around, is:

    unmatched_items_10 = [d for d in entries10 if d not in entries9]
    

    While more concise than your way of coding it, this has the same performance problem: it will take time proportional to the number of items in each list. If the lengths you’re interested in are about 9 or 10 (as those numbers seem to indicate), no problem.

    But for lists of substantial length you can get much better performance by sorting the lists and “stepping through” them “in parallel” so to speak (time proportional to N log N where N is the length of the longer list). There are other possibilities, too (of growing complication;-) if even this more advanced approach is not sufficient to get you the performance you need. I’ll refrain from suggesting very complicated stuff unless you indicate that you do require it to get good performance (in which case, please mention the typical lengths of each list and the typical contents of the dicts that are their items, since of course such “details” are the crucial consideration for picking algorithms that are a good compromise between speed and simplicity).

    Edit: the OP edited his Q to show what he cares about, for any two dicts d1 and d2 one each from the two lists, is not whether d1 == d2 (which is what the in operator checks), but rather d1[a]==d2[a] and d1[b]==d2[b]. In this case the in operator cannot be used (well, not without some funky wrapping, but that’s a complication that’s best avoided when feasible;-), but the all builtin replaces it handily:

    unmatched_items_10 = [d for d in entries10
                          if all(d[a]!=d1[a] or d[b]!=d2[b] for d2 in entries9)]
    

    I have switched the logic around (to != and or, per De Morgan’s laws) since we want the dicts that are not matched. However, if you prefer:

    unmatched_items_10 = [d for d in entries10
                          if not any(d[a]==d1[a] and d[b]==d2[b] for d2 in entries9)]
    

    Personally, I don’t like if not any and if not all, for stylistic reasons, but the maths are impeccable (by what the Wikipedia page calls the Extensions to De Morgan’s laws, since any is an existential quantifier and all a universal quantifier, so to speak;-). Performance should be just about equivalent (but then, the OP did clarify in a comment that performance is not very important for them on this task).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Python, compute list difference I have two lists For example: A =
I have two lists in python and I want to know if they intersect
I want to do this. I have two python lists, one larger than the
i want to find the intersection of two lists in python. i have something
I have two dictionaries in Python. One is a list of teams and their
Possible Duplicate: Python: How to find list intersection? I have two lists of data
If I have two dictionaries I'd like to combine in Python, i.e. a =
I have two lists of dictionaries list1 = [ {..}, {..}, ..] list2 =
I'm using Python 2.6, and I have two datasets, each being a list of
If I have two parallel lists and want to sort them by the order

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.