Python intersection of lists
In this post, you will learn how to find the intersection of lists using the Python programming language.
A list is an arranged collection of elements. It can contain a list of various types of data objects with each comma separated and enclosed within a square bracket. It is mutable, which means we can change the order of elements in a list. A list is widely used to store data in Python.
When we are working with multiple lists, we may come to a situation where we need to intersect two lists. In this post, we will use integer numbers in both lists, so that we can easily intersect them.
Intersection can be performed between two or more lists or sets. The intersection of two lists means we need to take all those elements which are common to both of the initial lists, i.e., it returns the elements which exist in all the lists.
Intersection of lists using set intersection() method
The set operation intersection takes only the elements that are in both sets. It contains the elements that the two sets have in common. The returned set contains only items that exist in both sets, or in all sets, if the comparison is done with more than two sets. Intersection of two given sets X and Y is a set which consists of all the elements which are common to both X and Y.
The set.intersection() method of Python returns a set of elements which are common in the given sets.
In the given example, we have taken two lists X and Y and used the set() method to convert X and Y from lists to sets. Then, we have used the built-in set's intersection() method. It returns a new set with the common values from the input sets.
X = [31, 42, 51, 11]
Y = [11, 33, 42, 52, 49,31]
intersection_set = set.intersection(set(X), set(Y))
#find intersection of X and Y
intersection_list = list(intersection_set)
print(intersection_list)
Output of the above code:
[42, 11, 31]
Intersection of lists using for loop
In the given Python program, we have used the for loop to find the intersection of two lists.
# Python program to get the intersection
# of two lists using for loop
def list_intersection(x, y):
z = [value for value in x if value in y]
return z
# Driver Code
list1 = [55, 23, 53, 66, 35, 64, 12, 15, 53]
list2 = [53, 12, 54, 35, 45, 11, 66, 28, 23]
print(list_intersection(list1, list2))
Output of the above code:
[23, 53, 66, 35, 12, 53]
Convert list to set and find the intersection
Here, we have used the set() method to convert list to set and then applied the AND (&) operator between the two sets to find the intersection.
# Python program to get the intersection
# of two lists
def intersection_list(list_x, list_y):
return list(set(list_x) & set(list_y))
X = [53, 55, 23, 52, 32, 40, 20, 10, 13]
Y = [20, 23, 54, 13, 40, 13, 66, 52, 26]
print(intersection_list(X, Y))
Output of the above code:
[40, 13, 20, 52, 23]
Intersection of lists using filter and lambda method
The function filter(function, list) offers an elegant way to filter out all the elements of a list, Only those elements will be included for which the function returns True value. This function will be applied to every element of the list.
A lambda function is a small anonymous function which takes any number of arguments, but can only have one expression. In the given example, we have used both filter and lambda to perform an intersection between two lists.
# Python program to get the intersection
# of two lists using filter and lambda
A = [53, 55, 23, 22, 32, 41, 20, 10, 13]
B = [20, 23, 54, 13, 40, 41, 66, 22, 26]
intersect = list(filter(lambda x:x in A,B))
print(intersect)
Output of the above code:
[20, 23, 13, 41, 22]
Intersection of lists using numpy.intersect1d() function
NumPy is the fundamental package for scientific computing with Python. It is a highly optimized library for numerical operations. We can use the intersect1d() function of numpy to find the intersection of two lists. This function returns the sorted, unique values that are present in both of the input lists.
# Python program to get the intersection
# of two lists using intersect1d() function
import numpy as np
A = [53, 55, 23, 22, 32, 41, 20, 10, 13]
B = [20, 23, 54, 13, 40, 41, 66, 22, 26]
intersect = list(np.intersect1d(A, B))
print(intersect)
Output of the above code:
[13, 20, 22, 23, 41]
Related Articles
Convert Python list to numpy arrayConvert string to list Python
Python program to list even and odd numbers of a list
Python loop through list
Sort list in descending order Python
Convert array to list Python
Python take screenshot of specific window
Web scraping Python BeautifulSoup
Check if two strings are anagrams Python
Python program to add two numbers
Print new line python
Python for loop index
Convert List to Dataframe Python
numpy random choice
Dictionary inside list python
Check if list is empty Python
Python raise keyword
Python program to get the largest number from a list
Python program to map two lists into a dictionary