Open In App

Python | Intersection of two lists

Last Updated : 28 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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)

Output
[9, 26, 11, 28]

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)

Output
[9, 11, 26, 28]

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)

Output
[9, 11, 26, 28]

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)

Output
[9, 11, 26, 28]

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.


Next Article
Practice Tags :

Similar Reads