Table of Contents

- What is the list intersection in Python?
- Ways to find the list intersection in Python
- Using the for loop to achieve list intersection in Python
- Using the set() function to find list intersection in Python
- Using the intersection() function to find list intersection in Python
- Using the filter() and lambda functions to find list intersection in Python
- Using the Counter class to find list intersection in Python
- Using the numpy.intersect1d() function to find list intersection in Python

A list is one of Python’s fundamental in-built data types and clusters several items in a single variable.

In this article, we will demonstrate the different ways available to retrieve the list intersection in Python.

## What is the list intersection in Python?

The intersection of two lists returns the elements that are present in both lists. There are several methods to find these elements.

## Ways to find the list intersection in Python

### Using the `for`

loop to achieve list intersection in Python

Instead of using any built-in functions available for this purpose, we can always create a simple user-defined function using the `for`

loop.

For example,

1 2 3 4 5 6 7 8 9 10 11 |
X = [1,3,5,7,9] Y = [1,5,10,15] def intsec(A, B): intsec = [] for val in A: if val in B: intsec.append(val) print(intsec) intsec(X,Y) |

The above code provides the following output:

##### Explanation

- We first define two lists named
`X`

and`Y`

in the above code. - We create a user-defined function that uses the for loop to traverse the list and find the common elements.
- We store these elements in a new list
`intsec`

and display them.

List comprehension can simplify the code and make the syntax compact in this case.

For example,

1 2 3 4 5 6 |
X = [1,3,5,7,9] Y = [1,5,10,15] Z = [val for val in X if val in Y] print(Z) |

The above code provides the following output:

This method increases the program’s efficiency, dropping its complexity to `O(n)`

.

### Using the `set()`

function to find list intersection in Python

We first convert the given lists into sets individually using the `set()`

method. Then we apply the intersection on the two sets using the intersection `&`

operator, which returns the intersection between the two sets.

See the code below.

1 2 3 4 5 6 |
X = [1,3,5,7,9] Y = [1,5,10,15] Z = list(set(X) & set(Y)) print(Z) |

The above code provides the following output:

##### Explanation

- The two lists
`X`

and`Y`

are defined. - Both the lists are converted into sets using the
`set()`

method. - We carry out the intersection operation using the
`&`

operator. - The result is stored in
`Z`

and is displayed.

### Using the `intersection()`

function to find list intersection in Python

Sets have an in-built method, `intersection()`

, which can quickly get the common items between sets. The `intersection()`

function will return the common values from both sets in a freshly generated set.

The lists need to be explicitly converted into sets first, as done in the previous method to use this function.

See the code below.

1 2 3 4 5 6 |
X = [1,3,5,7,9] Y = [1,5,10,15] Z = list(set(X).intersection(set(Y))) print(Z) |

The above code provides the following output:

### Using the `filter()`

and `lambda`

functions to find list intersection in Python

Python’s `filter()`

function can filter out elements of a given list based on some condition. Only the elements that satisfy this condition will be returned by this function.

Here, we will also take help from the `lambda`

function. A lambda function is a single-expression, one-line function that can take any number of arguments.

We use both these functions in the following example.

1 2 3 4 5 6 |
X = [1,3,5,7,9] Y = [1,5,10,15] Z = list(filter(lambda x:x in X,Y)) print(Z) |

The above code provides the following output:

##### Explanation

- We create the two lists.
- The
`filter()`

function takes each item of the first list and compares it with the items in the second list to check whether it is present in both lists. - If this is
`True`

, then we store the element in the list`Z`

.

The advantage of this method over its peers mentioned in this article is that we can also use it over sub-lists contained within the other lists.

### Using the `Counter`

class to find list intersection in Python

The `Counter`

is a class from the `collections`

module that keeps track of all the items’ frequency of occurrence in a given iterable. We can use it to return the list intersection of two lists.

For example,

1 2 3 4 5 6 7 8 |
from collections import Counter X = Counter([1,3,5,7,9]) Y = Counter([1,5,10,15]) Z = X & Y Z = list(Z.elements()) print(Z) |

The above code provides the following output:

##### Explanation

- We apply the
`Counter()`

constructor to both lists. - We perform the intersection operation on both
`X`

and`Y`

using the`&`

operator. - We store the elements of this class in
`Z`

using the`elements()`

function. - Finally, we display the common elements.

### Using the `numpy.intersect1d()`

function to find list intersection in Python

The `numpy.intersect1d`

function returns an array containing the intersection of two given arrays. We can convert this final result to a list using the `list()`

function.

For example,

1 2 3 4 5 6 7 |
import numpy as np X = [1,3,5,7,9] Y = [1,5,10,15] Z = list(np.intersect1d(X, Y)) print(Z) |

Output:

That’s all about list intersection in Python.