List intersection in python

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,

The above code provides the following output:

[1, 5]
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,

The above code provides the following output:

[1, 5]

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.

The above code provides the following output:

[1, 5]
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.

The above code provides the following output:

[1, 5]

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.

The above code provides the following output:

[1, 5]
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,

The above code provides the following output:

[1, 5]
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,

Output:

[1, 5]

That’s all about list intersection in Python.

Was this post helpful?

Leave a Reply

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