• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Intersection of Two Lists in Python

Intersection of Two Lists in Python

February 21, 2022 Leave a Comment

In Python, the easiest way to get the intersection of two lists is by using list comprehension to identify the values which are in both lists.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

intersection_of_lists = [x for x in list_1 if x in list_2]

print(intersection_of_lists)

#Output:
[3, 2, 1]

If you want to find the intersection of more than two lists, we can easily do that in Python with a simple function.

def intersection(lists):
    all_elements = lists[0]
    for i in range(1,len(lists)):
        all_elements = [x for x in all_elements if x in lists[i]]
    return all_elements

list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]

print(intersection([list1,list2,list3]))

#Output:
[2,5]

When working with multiple lists, it can be useful to find the entire collection of elements which exist in all of your lists. The intersection of two or more lists is a list of elements which are included in all of the lists.

So, for example, if we have a list A and a list B, then the intersection of A and B is a list of elements which are in both A and B.

We can get the intersection of two lists in Python easily.

To get the intersection of lists in Python, we can use list comprehension to identify the values which are in both lists.

Below is an example of how to get the intersection of two lists in Python.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

intersection_of_lists = [x for x in list_1 if x in list_2]

print(intersection_of_lists)

#Output:
[3, 2, 1]

If you’d like a result which is sorted, you can use the sorted() function or sort() function.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

intersection_of_lists = [x for x in list_1 if x in list_2]

print(sorted(intersection_of_lists))

#Output:
[1,2,3]

If you’d instead like to get the union of multiple lists, you can read our article about finding the union of lists in Python.

Finding the Intersection of Lists by Converting to Sets Python

The Python set data structure has many great capabilities for finding the union or intersection of multiple sets. We can convert our lists to sets and then use set functions to find the intersection of these lists.

Below is an example of using the intersection() set function to find the intersection of two lists in Python.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

intersection_of_lists = list(set(list_1).intersection(list_2))

print(intersection_of_lists)

#Output:
[1,2,3]

Another way you can find the intersection of two lists is by converting the lists to sets, and then using the & Python operator.

Below is another example in Python of how to find the intersection of two lists.

list_1 = [5,3,8,2,1]
list_2 = [9,3,4,2,1]

intersection_of_lists = list(set(list_1) & set(list_2))

print(intersection_of_lists)

#Output:
[1,2,3]

How to Find the Intersection of More Than Two Lists in Python

So far in this article, we’ve only found the intersection of two lists. We can easily find the intersection of more than two lists in Python.

To do so, we can define a function which takes in a number of lists, and then finds the intersection of all of the lists using list comprehension.

Since intersection of a list has the associative law, we can iterate over the lists and take intersections between the intersection of all lists to that point, and the next list.

Below is an example of how to find the intersection of more than two lists in Python.

def intersection(lists):
    all_elements = lists[0]
    for i in range(1,len(lists)):
        all_elements = [x for x in all_elements if x in lists[i]]
    return all_elements

list1 = [3,2,1,8,5,3,1]
list2 = [9,5,6,3,4,2]
list3 = [1,0,9,2,8,5,4]

print(intersection([list1,list2,list3]))

#Output:
[2,5]

Hopefully this article has been useful for you to learn how to use Python to get the intersection of lists.

Other Articles You'll Also Like:

  • 1.  Using Python to Get and Print First N Items in List
  • 2.  Repeat String with * Operator in Python
  • 3.  How to Clear Turtle Screen in Python with clear() Function
  • 4.  Get Name of Function in Python
  • 5.  Calculate Standard Deviation of List of Numbers in Python
  • 6.  Using Python to Reverse Tuple
  • 7.  Using Python to Remove Quotes from String
  • 8.  Find Index of Maximum in List Using Python
  • 9.  How to Check if a Dictionary is Empty in Python
  • 10.  Pandas Crosstab on Multiple Columns

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy