Python | Intersection of two lists
Last Updated :
28 Jan, 2025
The task of finding the intersection of two lists involves identifying the common elements between them. This means we want to extract the values that appear in both lists, while ignoring any duplicates or values that are unique to each list.
For example, if we have two lists [4, 9, 1, 17, 11] and [9, 9, 74, 21, 45], the intersection of these lists would be [9], as it is the only element that appears in both lists. The goal is to efficiently find and return these common elements.
Using set
Set is the most efficient method for finding the intersection of two lists. As it implemented hash tables, which allow for fast lookups and operations like intersection. By converting the lists into sets, we can utilize the & operator or the .intersection() to find common elements between them. This method performs in linear time, making it ideal for large lists.
Python
a = [4, 9, 1, 17, 11, 26, 28, 54, 69]
b = [9, 9, 74, 21, 45, 11, 63, 28, 26]
res = list(set(a) & set(b))
print(res)
Explanation: This code converts the lists a and b into sets, removing duplicates. It then finds the intersection of the two sets using the & operator, which returns the common elements. The result is converted back into a list.
Using list comprehension
List comprehension is a concise and readable way to filter elements in one list based on their presence in another. In this case, we can loop through the first list and check if each element exists in the second list .This approach is straightforward but can be inefficient for large lists.
Python
a = [4, 9, 1, 17, 11, 26, 28, 54, 69]
b = [9, 9, 74, 21, 45, 11, 63, 28, 26]
res = [value for value in a if value in b]
print(res)
Explanation: List comprehension iterates through each element of list a and checks if that element is present in list b using the in operator. If the element is found in b, it is added to the resulting list res.
Using filter()
filter() allows us to filter elements from an iterable based on a condition. In this case, we use a lambda function to define the condition where the element from the first list should also be present in the second list. This approach is functional and may appeal to those familiar with functional programming paradigms.
Python
a = [4, 9, 1, 17, 11, 26, 28, 54, 69]
b = [9, 9, 74, 21, 45, 11, 63, 28, 26]
res = list(filter(lambda x: x in b, a))
print(res)
Explanation: filter() with a lambda function to check if each element from list a exists in list b. The filtered elements are then converted to a list and printed. This finds the common elements .
Using counter
Counter from the collections module is used for counting the occurrences of elements in a list. If we want to find the intersection of two lists, accounting for duplicates, Counter can be very useful. It creates frequency distributions of the elements in both lists and computes the intersection by finding the minimum frequency for each common element.
Python
from collections import Counter
a = [4, 9, 1, 17, 11, 26, 28, 54, 69]
b = [9, 9, 74, 21, 45, 11, 63, 28, 26]
res = list((Counter(a) & Counter(b)).elements())
print(res)
Explanation: Counter count the frequency of elements in both lists, then performs an intersection with the & operator, which keeps the minimum count of common elements .elements() returns these elements and the result is converted to a list.
Similar Reads
Python | Intersection of two nested list Finding the intersection of two nested lists means figuring out which elements or sub-lists are present in both lists. The lists can have different structures and levels of complexity. In this article, we'll explore various methods to efficiently identify the common elements between two nested lists
3 min read
Intersection of two String - Python We are given two strings and our task is to find the intersection of characters between them. This means we need to identify which characters are common in both strings, regardless of their position. For example, if we have strings like "GeeksforGeeks" and "Codefreaks", the common characters would b
3 min read
Python | Sympy Line.intersection() method In Sympy, the function intersection() is used to find the intersection with another geometrical entity. Syntax: Line.intersection(o) Parameters: o: Point or LinearEntity Returns: intersection: list of geometrical entities Example #1: Python3 1== # import sympy and Point, Line from sympy import Point
1 min read
Python Set - intersection_update() Method Python intersection_update() method is used to update a set with common elements only of all the sets passed in parameter of intersection_update() method. Python set intersection_update() Method Syntax: Syntax: set.intersection_update(set1,set2,set3,...........,set n) Parameters:Â One or more sets R
2 min read
Python â Sympy Polygon.intersection() Method In Sympy, the function Polygon.intersection() is used to get the intersection of a given polygon and the given geometry entity. The geometry entity can be a point, line, polygon, or other geometric figures. The intersection may be empty if the polygon and the given geometry entity are not intersecte
2 min read