You are currently viewing Python List Intersection

How to get the common elements from the list similar to the intersection in Python? If you want to return only common elements from the given lists, then you can use & operator and intersection() method by converting the lists to sets. List in python is a one-dimensional data structure. It is mutable and we can access elements very quickly. Intersection means retrieving common elements from the given datasets.

Advertisements

The Numpy module also supports intersect1d() method that returns common elements from the given two lists. List comprehension can also be used to return the common elements from the given lists by utilizing in operator. Similarly, we can also use for loop.

1. Quick Examples of Intersection of Lists

Following are quick examples of how to return common elements from the given lists.


# Quick Examples

# Consider two lists
scores1 = [100,200,300,400,500]
scores2 = [200,300,600,700,900]

# Using List comprehension
result = [value for value in scores1 if value in scores2]

# Using & operator by converting List to Set
result = list(set(scores1) & set(scores2))

# Using intersection() by converting List to Set
result = list(set(scores1).intersection(set(scores2)))

# Using filter()
result = list(filter(lambda i: i in scores1, scores2))

# Using numpy.intersect1d()
import numpy as np
result = np.intersect1d(scores1,scores2)

# Using for loop
result=[]
for value in scores1:
   if (value in scores1):
     result.append(value)
print("List 1 ∩ List 2: ",result)

2. Python List Intersection by Comprehension

To get the intersection elements from two lists you can use the list comprehension in Python, first we will iterate all the elements in the first list and check if the element exists in the second list using if condition; if the element exists in the second then, we will store that element in a list.

2.1 Syntax

Let’s see how to perform an Intersection operations with a list comprehension.


# Here, mylist1 is the first list
# and mylist2 is the second list.
# value will act as iterator.
[value for value in mylist1 if value in mylist2]

2.2 List Intersection Example

Create two lists with some integers and return common elements using List comprehension.


# Consider two lists
scores1 = [100,200,300,400,500]
scores2 = [200,300,600,700,900]

# Intersection using list comprehension
list3 = [value for value in scores1 if value in scores2]
print("List 1 ∩ List 2: ",list3)

# Output:
# List 1 ∩ List 2:  [200, 300]

In the above example, only 2 elements are common in both lists hence 200 and 300 are returned as intersection elements.

3. Find Intersection using Looping

To get an intersection on two or more lists in Python, first, we will iterate all the elements in the first list using for loop and check if the element exists in the second list using the if condition inside a loop. If the element exists in the second list then, we will store that element in a new list by appending elements into it.

3.1 for loop Syntax

Let’s see the syntax of how to perform an Intersection operation with for loop.


# Here, mylist1 is the first list
# and mylist2 is the second list.
# value will act as iterator.
result=[]
for value in mylist1:
   if (value in mylist2):
     result.append(value)

3.2 Intersection Example

Create two lists with some integers and return common elements using for loop.


# Consider two lists
scores1 = [100,200,300,400,500]
scores2 = [200,300,600,700,900]

# Using for loop
result=[]
for value in scores1:
   if (value in scores2):
     result.append(value)
print("List 1 ∩ List 2: ",result)

# Output:
# List 1 ∩ List 2:  [200, 300]

Only 2 elements are common in both lists i.e 200 and 300.

4. Using & operator

Operator & is used to perform intersection on two sets. It won’t work on lists. So we need to convert the list to set using set() method and then we have to apply & operator. Finally, we need to convert the set back to a list using list() function.

4.1 & operator Syntax

Following is the syntax of the set intersection.


# Syntax
list(set(mylist1) & set(mylist2) & ...)

Here, mylist1 is the first list and and mylist2 is the second list.

4.2 Example

Create three lists with some integers and perform the intersection operation with & operator.


scores1 = [100,300,400,500]
scores2 = [200,400,600,700,900]
scores3 = [200,300,600,700,900]

# Using & operator
list3 = list(set(scores1) & set(scores2) & set(scores3))
list4 = list(set(scores1) & set(scores3))
print("List 1 ∩ List 2 ∩ List 3: ",list3)
print("List 1 ∩ List 3: ",list4)

# Output:
# List 1 ∩ List 2 ∩ List 3:  []
# List 1 ∩ List 3:  [300]
  1. There are no common elements among the three lists, hence list3 is empty. And 300 is returned to list3 as an intersection for the second example as it is the only element that is common in the first and third lists.

5. Using intersection() with Python List

intersection() is used to perform intersection operations on python sets, it won’t work on lists. So we need to convert the list to a set using set() method and then we have to apply intersection(). Finally, we need to convert the set back to a list using the list() function.

5.1 intersection() Syntax

Let’s see how to perform an intersection operation using intersection() method.


# Here, mylist1 is the first list
# and mylist2 is the second list.
list(set(mylist1).intersection(set(mylist2),...))

5.2 Examples

Example 1: Create two lists with some strings and perform an intersection operation using the intersection() method.


fruits1 = ['apple', 'mango', 'guava', 'lemon']
fruits2 = ['lemon','papayya', 'pear']

# Using intersection()
list3 = list(set(fruits1).intersection(set(fruits2)))
print("List 1 ∩ List 2: ",list3)

# Output:
# List 1 ∩ List 2:  ['lemon']

lemon is the only element that exists in both lists hence it is returned as an intersection.

Example 2: Create three lists with some integers and perform an intersection operation among them using the intersection() method.


# Crate lists
scores1 = [100,300,400,500]
scores2 = [200,400,600,700,900]
scores3 = [200,300,600,700,900]

# Using intersection()
list3 = list(set(scores1).intersection(set(scores2),set(scores3)))
print("List 1 ∩ List 2 ∩ List 3: ",list3)

# Output:
# List 1 ∩ List 2 ∩ List 3:  []

There is no element common in all three lists.

6. Using numpy.intersect1d()

numpy.intersect1d() is available in the python numpy module which will perform intersection on two or more lists. It is very important to import the numpy module.

6.1 intersection() Syntax

Following is the syntax of the numpy.intersect1d() method.


# Syntax
numpy.intersect1d(mylist1,mylist2,....)

6.2 Example

Create two lists with some integers and perform the intersection operation using the numpy.intersect1d() method.


# Import numpy
import numpy as np
scores1 = [100,200,300,400,500]
scores2 = [200,300,600,700,900]

# Using numpy.intersect1d()
print("List 1 ∩ List 2: ",np.intersect1d(scores1,scores2))

# Output:
# List 1 ∩ List 2:  [200 300]

Here, 200 and 300 are common in both lists.

7. List Intersection Using filter()

We can pass a lambda expression that will return the elements present in both lists as an intersection in python. It will use the in operator which will check whether each element exists in both lists or not. If the element exists in both the lists, it will filter and return in a list, We are using the list() function to return all the filtered values in a list.

7.1 filter() Syntax

Syntax of list intersection using lambda.


# Syntax
list(filter(lambda i: i in mylist1, mylist2))

7.2 List Intersection Example

Create two lists with some strings and perform the intersection operation using the filter() method.


fruits1 = ['apple', 'mango', 'guava', 'lemon']
fruits2 = ['lemon','papayya', 'pear']

# Using filter()
list3 = list(filter(lambda i: i in fruits1, fruits2))
print("List 1 ∩ List 2: ",list3)

# Output:
# List 1 ∩ List 2:  ['lemon']

lemon‘ is only the element present in both lists.

8. Conclusion

We discussed several different ways to perform intersection operations on the lists in python. For each case, we explained one example by taking a simple list of elements. We utilize filter(), for loop, and List comprehension to perform intersection operation. make sure that you need to convert your lists to sets while using & operator and intersection() method as these work only on sets.

References