Find the intersection of two lists in Python
In this post, you will learn different ways to find the intersection of two lists using the Python programming language.
A list is an arranged collection of elements. It is used to store collections of data. It can contain a list of various types of data objects with a comma separated and enclosed within a square bracket.
The intersection of two lists contains the elements that the two lists have in common. The returned list contains only items that exist in both lists, or in all lists, if the comparison is done with more than two lists. There are various ways through which we can perform the intersection of two lists. Here we have mentioned most of them.
Using set.intersection() method to find the intersection
For this, we need to convert the lists to sets using the set(iterable) method. In the given example, we have taken two lists and converted them to sets. We have used the intersection() method to find the intersection of sets. Finally, we have passed this resulting set to the list() function to convert it back to a list and assign the resulting list to the result.
#Intersection of two lists
list_x = [11, 23, 11, 2, 3, 4, 5]
list_y = [3, 11, 5, 2, 7, 23]
set1 = set(list_x)
set2 = set(list_y)
intersect = set1.intersection(set2)
result = list(intersect)
print(result)
Output of the above code-
[2, 3, 5, 11, 23]
Using & operator to find the intersection
Instead of using the intersection() method, we can also use the & operator to find the intersection between the two sets. This operator returns a new set with elements common to both set1 and set2.
#Intersection of two lists
list_x = [22, 53, 23, 57, 33, 64, 11, 7]
list_y = [3, 11, 53, 2, 7, 23]
set1 = set(list_x)
set2 = set(list_y)
result = list(set1 & set2)
print(result)
Output of the above code-
[11, 23, 53, 7]
Find the intersection of lists without using any in-built method
This is the simplest way in Python to find the intersection of two lists without using any in-built method.
# Intersection of two lists
def list_intersection(x, y):
result = [value for value in x if value in y]
return result
list_x = [22, 53, 23, 57, 33, 64, 11, 7]
list_y = [3, 11, 53, 2, 7, 23]
print(list_intersection(list_x, list_y))
Output of the above code-
[53, 23, 11, 7]
Using hybrid method to find the intersection
Here, we have used the hybrid method to find the intersection of lists. This is a much more efficient way to find the intersection, as the complexity of the program falls to O(n).
def intersection(lst1, lst2):
# Use of hybrid method
temp = set(lst2)
lst3 = [value for value in lst1 if value in temp]
return lst3
# Driver Code
list_x = [21, 3, 23, 27, 22, 92, 11, 2]
list_y = [3, 11, 53, 2, 7, 23]
print(intersection(list_x, list_y))
Output of the above code:
[3, 23, 11, 2]
Related Articles
Find the stop words in nltk Python
Python OpenCV Histogram of Grayscale Image
Python OpenCV Image Filtering
Eye Detection Program in Python OpenCV
Python loop through list
Difference between tuple and list in Python
Convert string to list Python
Convert Python list to numpy array
Install NLTK for Python on Windows 64 bit
Python weather api
Python loop through list
Python compare two lists
Remove element from list Python
Python iterate list with index
Python program to sum all the numbers in a list
Python print without newline
Python iterate list with index
Python add list to list
Python random choice
Python dict inside list
Remove character from string Python
Python raise keyword