# Find Intersection of Two Lists in Python (3 Examples)

Hi! This tutorial will demonstrate how to **get the intersection of two lists** in the Python programming language.

Here is an overview:

Let’s dive into the Python code!

## Create Example Python Lists of Integers

Here, we will create two example Python lists of integers whose intersection we are going to determine in this tutorial.

Therefore, in your preferred Python programming IDE, run the lines of code below to create the example lists of integers:

list1 = [1, 2, 3, 4, 5] list2 = [4, 5, 6, 7, 8] print(list1) print(list2) # [1, 2, 3, 4, 5] # [4, 5, 6, 7, 8] print(type(list1)) print(type(list2)) # <class 'list'> # <class 'list'>

With the example list of integers created, let us now see ways we can get the intersection of the two lists.

## Example 1: Get Intersection of Two Lists Using set() Function and “&” Operator

In this first example, we are going to get the intersection of the two lists using the set() function and & operator:

intersection = set(list1) & set(list2) print(intersection) # {4, 5}

Here, we used the `set()`

function to first convert the lists to sets, which eliminated duplicate elements in each list. Then, we used the “&” operator to find the intersection of both sets, i.e. the elements common to both sets.

## Example 2: Get Intersection of Two Lists Using intersection() Method

In this next example, we will use the intersection() method to determine the intersection of the two lists:

intersection = set(list1).intersection(list2) print(intersection) # {4, 5}

In this example, we first converted each list to a set and then used the `intersection()`

method to find the intersection of the two sets.

The `intersection()`

method is a built-in set method that returns a new set containing the common elements between two sets.

## Example 3: Get Intersection of Two Lists Using for Loop & Conditional “if” Statement

In this last example, we will use a for loop and a conditional “if” statement to find the intersection of the two lists:

intersection = [] for i in list1: if i in list2: intersection.append(i) print(intersection) # [4, 5]

Here, we used a for loop to iterate through the elements in list1. Then, if any element in list1 is also in list2, it is appended to the empty intersection list using the append() method and the list is returned.

So, with that, we have demonstrated, with 3 examples, how to find the intersection of two lists in Python. I do hope you found this tutorial helpful!

## Video, Further Resources & Summary

Do you need more explanations on how to find the intersection of two lists in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

In the video, we explain in some more detail how to find the intersection of two lists in Python.

*The YouTube video will be added soon.*

Furthermore, I encourage you to check out other interesting Python list tutorials on Statistics Globe, starting with these:

- count() Method for Lists in Python (2 Examples)
- Append to 2D List in Python (3 Examples)
- List Comprehension in Python
- Learn Python Programming
- Transpose 2D List in Python (3 Examples)

This post has shown how to **find the intersection of two lists in Python**. In case you have further questions, you may leave a comment below.

This page was created in collaboration with Ifeanyi Idiaye. You might check out Ifeanyi’s personal author page to read more about his academic background and the other articles he has written for the Statistics Globe website.