Lists are data structures that store multiple variables or objects in a single variable. You can append, or remove elements from a single list. Suppose you have two lists and want to find the intersection of these two how you will do so. In this post, you will learn the various methods to find the Intersection of Two Lists in Python.
Methods to find Intersection of Two Lists in Python
Below are the methods to find the intersection of two lists in Python.
Method 1: Using a loop and conditional statements
Below are the steps to follow for this method.
- Iterate over each element in the first list.
- Check if the element is present in the second list.
- If it is, add it to a new list that holds the intersection elements.
def intersection(list1, list2):
return [ele for ele in list1 if ele in list2]
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[20, 40, 60]
Method 2: Using the set data structure
Here you have to first convert both lists into sets. After that take the intersection of these sets.
def intersection(list1, list2):
return list(set(list1) & set(list2))
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[40, 20, 60]
Method 3: Using the intersection method of sets
In this method, you will first create two sets from the input lists and after that, you will use the intersection method to get their common elements.
def intersection(list1, list2):
set1 = set(list1)
set2 = set(list2)
return list(set1.intersection(set2))
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[40, 20, 60]
Method 4: Using the filter function
You can also use the filter function with lambda to find the intersection. Use the below steps.
- Define a lambda function that checks if an element is present in the second list.
- Then Use the filter function with the lambda function and the first list as arguments.
def intersection(list1, list2):
return list(filter(lambda x: x in list2, list1))
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[20, 40, 60]
Method 5: Using list comprehension with the in operator:
In this method iterate over each element in the first list. After that use the in operator to check if the element is present in the second list or not.
def intersection(list1, list2):
return [element for element in list1 if element in list2]
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[20, 40, 60]
Method 6: Using the numpy library
You can also use the numpy python package to find the intersection. Just follow the below steps.
- First, convert both lists into numpy arrays.
- Then use the intersect1d function from the numpy library to get the common elements.
import numpy as np
def intersection(list1, list2):
array1 = np.array(list1)
array2 = np.array(list2)
return np.intersect1d(array1, array2).tolist()
list1 =[10,20,30,40,60]
list2=[40,20,50,60,70]
print(intersection(list1,list2))
Output
[20, 40, 60]
Conclusion
The intersection of two lists is done to find the unique element present in both lists. These are the methods to find the intersection. You can use any method as per convenience.
Leave a Reply