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 5951253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T17:30:38+00:00 2026-05-22T17:30:38+00:00

What is the easiest way to compare the 2 lists/sets and output the differences?

  • 0

What is the easiest way to compare the 2 lists/sets and output the differences? Are there any built in functions that will help me compare nested lists/sets?

Inputs:

First_list = [['Test.doc', '1a1a1a', 1111], 
              ['Test2.doc', '2b2b2b', 2222],  
              ['Test3.doc', '3c3c3c', 3333]
             ]  
Secnd_list = [['Test.doc', '1a1a1a', 1111], 
              ['Test2.doc', '2b2b2b', 2222], 
              ['Test3.doc', '8p8p8p', 9999], 
              ['Test4.doc', '4d4d4d', 4444]]  

Expected Output:

Differences = [['Test3.doc', '3c3c3c', 3333],
               ['Test3.doc', '8p8p8p', 9999], 
               ['Test4.doc', '4d4d4d', 4444]]
  • 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-22T17:30:38+00:00Added an answer on May 22, 2026 at 5:30 pm

    So you want the difference between two lists of items.

    first_list = [['Test.doc', '1a1a1a', 1111], 
                  ['Test2.doc', '2b2b2b', 2222], 
                  ['Test3.doc', '3c3c3c', 3333]]
    secnd_list = [['Test.doc', '1a1a1a', 1111], 
                  ['Test2.doc', '2b2b2b', 2222], 
                  ['Test3.doc', '8p8p8p', 9999], 
                  ['Test4.doc', '4d4d4d', 4444]]
    

    First I’d turn each list of lists into a list of tuples, so as tuples are hashable (lists are not) so you can convert your list of tuples into a set of tuples:

    first_tuple_list = [tuple(lst) for lst in first_list]
    secnd_tuple_list = [tuple(lst) for lst in secnd_list]
    

    Then you can make sets:

    first_set = set(first_tuple_list)
    secnd_set = set(secnd_tuple_list)
    

    EDIT (suggested by sdolan): You could have done the last two steps for each list in a one-liner:

    first_set = set(map(tuple, first_list))
    secnd_set = set(map(tuple, secnd_list))
    

    Note: map is a functional programming command that applies the function in the first argument (in this case the tuple function) to each item in the second argument (which in our case is a list of lists).

    and find the symmetric difference between the sets:

    >>> first_set.symmetric_difference(secnd_set) 
    set([('Test3.doc', '3c3c3c', 3333),
         ('Test3.doc', '8p8p8p', 9999),
         ('Test4.doc', '4d4d4d', 4444)])
    

    Note first_set ^ secnd_set is equivalent to symmetric_difference.

    Also if you don’t want to use sets (e.g., using python 2.2), its quite straightforward to do. E.g., with list comprehensions:

    >>> [x for x in first_list if x not in secnd_list] + [x for x in secnd_list if x not in first_list]
    [['Test3.doc', '3c3c3c', 3333],
     ['Test3.doc', '8p8p8p', 9999],
     ['Test4.doc', '4d4d4d', 4444]]
    

    or with the functional filter command and lambda functions. (You have to test both ways and combine).

    >>> filter(lambda x: x not in secnd_list, first_list) + filter(lambda x: x not in first_list, secnd_list)
    
    [['Test3.doc', '3c3c3c', 3333],
     ['Test3.doc', '8p8p8p', 9999],
     ['Test4.doc', '4d4d4d', 4444]]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the easiest way to compare strings in Python, ignoring case? Of course
What would be the easiest way to detach a specific JPA Entity Bean that
What would be the easiest way to compare multiple arrays, and remove duplicates? So
What's the easiest way of decoding a string such that: 'Bayern München' -> 'Bayern
The easiest way to see this problem will be to run the sample project
What's the easiest way in Java to compare the contents of two ByteBuffers to
The easiest way to think of my question is to think of a single,
The easiest way to explain this problem is to show you some code: Public
What would be the easiest way to separate the directory name from the file
In TFS whats the easiest way of linking a backlog item to a large

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.