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?


import_contacts

You may also like:

Related Posts

  • 24 May

    Replace Comma with Space in List in Python

    Table of ContentsHow to replace comma with space in list in PythonUsing the replace() function to replace comma with space in list in PythonUsing the re.sub() function to replace comma with space in list in PythonUsing the split() and join() functions to replace comma with space in list in Python.ConclusionWas this post helpful? In this […]

  • 19 May

    Compare list elements with each other in Python

    Table of ContentsCompare all elements in list PythonWays to compare list elements with each other in PythonUsing the for loop to compare all elements in list PythonUsing the itertools.combinations() function to compare all elements in list PythonConclusionWas this post helpful? In this post, we will see how to compare list elements with each other in […]

  • 18 May

    How to Deep Copy a List in Python

    Table of ContentsWhat is a list in Python?What are shallow copy and deep copy operations in Python?Shallow CopyDeep CopyHow to deep copy a list in Python?Using the deepcopy() function from the copy module to deep copy a list in Python.Using a user-defined function to deep copy a list in Python.Using list comprehension to deep copy […]

  • 18 May

    Convert Generator to List in Python

    Table of ContentsLists and Generators in PythonWays to convert generator to list in PythonUsing the list() function to convert generator to list in PythonUsing the unpack operator * to convert generator to list in PythonUsing the list comprehension to convert generator to list in PythonUsing the append() function to convert generator to list in PythonConclusionWas […]

  • Create a list from 1 to 100 in Python
    03 May

    Create a List from 1 to 100 in Python

    Table of ContentsWays to create a list from 1 to 100 in PythonUsing the range() function to create a list from 1 to 100 in PythonUsing the numpy.arange() function to create a list from 1 to 100 in PythonUsing the for loop with range() to create a list from 1 to 100 in PythonConclusionWas this […]

  • 07 January

    How to compare lists in Python

    Table of ContentsPython compare listsHow to compare lists in PythonUsing the sort() function and == operatorUsing the map() and reduce() functionsUsing the set() function and == operatorUsing the for loopUsing the set() function and - operatorUsing the collections.Counter() class and == operatorUsing the zip(), sum(), and len() functionsUsing the cmp() functionConclusionWas this post helpful? In […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.