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

  • SEARCH
  • Home
  • 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 442943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:07:18+00:00 2026-05-12T21:07:18+00:00

I have two lists of dictionaries list1 = [ {..}, {..}, ..] list2 =

  • 0

I have two lists of dictionaries

list1 = [ {..}, {..}, ..]
list2 = [ {..}, {..}, ..]

I want to remove the dictionaries in list1 which are in list2. I had a similar problem where I had a list of lists instead of a dictionary and it is solved here

python function slowing down for no apparent reason

If I use the same code which is,

def removeDups(list1, list2):
    list2_set = set([tuple(x) for x in list2])
    diff = [x for x in list1 if tuple(x) not in list2_set]

    return diff

I do not get correct results since dictionaries like

{key1:'a', key2:'b'} and 
{key2:'b', key1:'a'}

which are the same are actually considered as different. How can I change the code or what can I do to remove dictionaries from list1 that appear in list2?

  • 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-12T21:07:19+00:00Added an answer on May 12, 2026 at 9:07 pm

    You can’t use dicts in sets because they’re mutable and don’t have stable identities. You can work around that by making a tuple out of their items. Note that simply wrapping a dict in a tuple doesn’t get around the fact that distinct dicts will still appear to be distinct objects even if they contain the same items.

    To turn two “equivalent” dicts into equal objects, take all of their items, sort the items, and then stuff them into a tuple: tuple(sorted(map.items())). Those tuples will properly compare equal to each other if they contain the same items, no matter the order of the original dict.

    def removeDups(list1, list2):
        set1 = set(tuple(sorted(x.items())) for x in list1)
        set2 = set(tuple(sorted(x.items())) for x in list2)
    
        return set1 - set2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is my problem: I have two dictionaries with identical structures: Dictionary<string, List<Object>> Existing
I have two lists which are guaranteed to be the same length. I want
I have two lists (list1 and list2) containing references to some objects, where some
I have two complex dictionaries in the form Dictionary<string, Dictionary<string, Dictionary<string, List<string>>>> So as
I have a Dictionary of dictionaries : SortedDictionary<int,SortedDictionary<string,List<string>>> I would like to merge two
I have two lists of entities. Imagine list1 is remote and list2 is local
I have two Python lists of dictionaries, entries9 and entries10 . I want to
I have two lists of custom objects and want to update a field for
I have two lists of objects. Each list is already sorted by a property
I have two lists and I want to select items that are not in

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.