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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:11:36+00:00 2026-06-10T14:11:36+00:00

Possible Duplicate: Common elements comparison between 2 lists I have two Lists: [Apples, Bananas,

  • 0

Possible Duplicate:
Common elements comparison between 2 lists

I have two Lists:

[Apples, Bananas, Pears]
[Kiwis, Bananas, Apples]

I would like to get only the elements that both lists share. There have to be some built-in functions in python for that.

Result:

[Apples, Bananas]
  • 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-06-10T14:11:38+00:00Added an answer on June 10, 2026 at 2:11 pm

    Original Answer:

    [x for x in list1 if x in list2]
    

    In response to the comment question about preserving order:

    That list comprehension is just a shorthand version of

    intersection = []
    for x in list1:
        if x in list2: #Note that using 'in' involves looping over list2
            intersection.append(x)
    

    With this expanded version, it’s a little easier to see what is going on. The output list is exactly list1 with the elements that are in list2 removed. So it will preserve the order of list1. For example, if list1 = [1,2,3] and list2 = [3,2,5], the output of the list comprehension will be [2,3]. If the position of the lists was reversed like this

    [x for x in list2 if x in list1]
    

    Then the order of list2 would be preserved in the output, giving us [3,2].

    Also, in a possibly undesirable side effect, this means that this method will contain duplicate elements of list1. For example:

    >>> [x for x in [1,2,3,3,3] if x in [2,3]]
    [2, 3, 3, 3]
    

    So for your example, this happens:

    >>> [fruit for fruit in ["Apples", "Bananas", "Pears"] if fruit in ["Kiwis", "Bananas", "Apples"]]
    ['Apples', 'Bananas']
    

    But if the lists are flipped, then the output is reversed:

    >>> [fruit for fruit in ["Kiwis", "Bananas", "Apples"] if fruit in ["Apples", "Bananas", "Pears"]]
    ['Bananas', 'Apples']
    

    So, in general, the sets solution is better because it is more efficient and because often you won’t want the duplicate elements. However, if you want to preserve order, this is the way to go. (If you want to preserve order and not have duplicates, you can just use this method and then strip out the duplicates, depending on whether you want the earlier duplicate or a later one to remain.)

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

Sidebar

Related Questions

Possible Duplicate: Are do-while-false loops common? Is there a reason to have code like:
Possible Duplicate: Comparing two arrays & get the values which are not common I
Possible Duplicate: Where to put common writable application files? In my application I have
Possible Duplicate: Java: Checking equality of arrays (order doesnt matter) I have two arrays
Possible Duplicate: Can you share a file and it's history between two git repositories?
Possible Duplicate: What are common concurrency pitfalls? I have basic knowledge of threading, nothing
Possible Duplicate: How to find the nearest common ancestors of two or more nodes?
Possible Duplicate: Reference unit tests for common data structures? I'm trying to implement the
Possible Duplicate: Good STL-like library for C Are there any open source C libraries
Possible Duplicate: Why C# implements methods as non-virtual by default? It would be much

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.