In this tutorial, you’ll learn how to use Python to find the intersection between two lists. **Intersection means finding the common elements between two lists**. Typically, intersection is `set-based`

, meaning that the values are unduplicated.

You will learn some naive methods to find intersection between two Python lists, including for loops and list comprehensions, using the set `.intersection()`

method and using numpy.

**The Quick Answer: Use Python Set Operations**

## Using a Python For Loop to Find Intersection Between Two Lists

Using a Python for loop is an easy, intuitive way to find the intersection between two lists. The benefit of understandability here comes at a performance hit with lists of much larger sizes.

What we’ll do, is loop over each item in a list and see if it exists in the other. If it does, then we append it to a new list. If it doesn’t, then we’ll do nothing.

Let’s take a look at what this code would look like:

```
# Find intesection between two Python lists using a for loop
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['b', 'd', 'e', 'f', 'g']
intersection = list()
for item in list1:
if item in list2:
intersection.append(item)
print(intersection)
# Returns: ['b', 'd', 'e']
```

In the next section, you’ll learn how to turn this for loop into a Python list comprehension.

**Want to learn more about Python for-loops?** Check out my in-depth tutorial that takes your from beginner to advanced for-loops user! Want to watch a video instead? Check out my YouTube tutorial here.

## Using List Comprehensions to find the Intersection Between Two Python Lists

In many cases, a Python for loop can be turned into a list comprehension. The benefit of doing this is the ease of coding it out. Python list comprehensions comes with some key benefits:

- You don’t need to instantiate a new, empty list
- The code generally only spans a single line, rather than multiple lines

Essentially, the algorithm we’ll follow is the same as the for loop: we loop over each item and see if it exists in the other list. If it does, then we add it to our new list.

Let’s see what this would look like in Python:

```
# Find intesection between two Python lists using a list comprehension
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['b', 'd', 'e', 'f', 'g']
intersection = [item for item in list1 if item in list2]
print(intersection)
# Returns: ['b', 'd', 'e']
```

Similar to the for loop method, as your list sizes grow, this can encounter some performance hiccups. This is where the next method comes into play. In the next section, you’ll learn how to use Python set methods to find the intersection between two lists.

**Want to learn more about Python list comprehensions?** Check out this in-depth tutorial that covers off everything you need to know, with hands-on examples. More of a visual learner, check out my YouTube tutorial here.

## Using Python Set Operations to find the Intersection Between Two Lists

Python sets are similar to lists, but they have a number of key differences. While lists are ordered, sets are unordered; while lists can hold duplicate items, sets cannot.

Sets also come with a number of helpful methods. In the example below, we’ll use the `.intersection()`

method to generate a set of items that exist between both sets.

Let’s take a look at what our code looks like:

```
# Find intesection between two Python lists using set.intersection()
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['b', 'd', 'e', 'f', 'g']
intersection = list(set(list1).intersection(set(list2)))
print(intersection)
# Returns: ['b', 'd', 'e']
```

Let’s take a look at what we’ve done:

- We converted both lists to sets
- We then applied the
`.intersection()`

method - Finally, we convert the final set back to a list

In the next section, you’ll learn how to use the `&`

operator to find the intersection between two lists.

**Want to learn how to use the Python zip() function to iterate over two lists?** This tutorial teaches you exactly what the

`zip()`

function does and shows you some creative ways to use the function.## Using the And Operator to Find Python List Intersection

In the example above, you learned how to use a set method, the `.intersection()`

method, to find the intersection between two lists. We can also use a boolean method, using the `&`

operator, to find the intersection between two sets.

Using this operator evaluates whether items exist in both sets and returns items that meet the criteria.

Let’s take a look at what this look like:

```
# Find intesection between two Python lists using &
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['b', 'd', 'e', 'f', 'g']
intersection = list(set(list1) & set(list2))
print(intersection)
# Returns: ['b', 'd', 'e']
```

In the next example, you’ll learn how to use `numpy`

to check for items that exist in two lists.

Check out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas!

## Using Numpy to Find the Intersection Between Two Lists

Numpy is a helpful library that uses a list-like object called the `np.array`

. These arrays are similar to Python lists but allow us to work with numpy array methods. One of these methods is the `intersect1d()`

method, which, well, allows us to find the intersection between 1 dimensional arrays.

Let’s take a look at what this code looks like:

```
# Find intesection between two Python lists using numpy
list1 = ['a', 'b', 'c', 'd', 'e']
list2 = ['b', 'd', 'e', 'f', 'g']
intersection = list(np.intersect1d(list1, list2))
print(intersection)
# Returns: ['b', 'd', 'e']
```

Let’s explore what we’ve done here:

- We pass our two lists into the
`np.intersect1d()`

method - This returns an
`np.array`

that only includes the intersection between the two lists - Finally, we convert this array back into a list

**Need to check if a key exists in a Python dictionary?** Check out this tutorial, which teaches you five different ways of seeing if a key exists in a Python dictionary, including how to return a default value.

## Conclusion

In this post, you learned how to use Python to find the intersection between two lists. You learned that the easiest way to do this is to use Python set operations – in particular, using the `.intersection()`

method. You also learned some naive implementations, including using for loops and list comprehensions. These methods, while not always the fastest to write, allow you to gain a strong understanding of how these algorithms work. Finally, you learned how to use `numpy`

to find the intersection between two lists.

To learn more about the set `.intersection()`

method, check out the official documentation here.

Pingback: Python: Combine Lists - Merge Lists (8 Ways) • datagy