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 lists
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 [
4 min read
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
Python | Intersection of two String
One of the string operations can be computing the intersection of two strings i.e, output the common values that appear in both the strings. There are various ways in Python, through which we can perform the Intersection of two strings. Method #1 : Naive Method Create an empty string and check for n
3 min read
Python List of Lists
A list of lists in Python is a collection where each item is another list, allowing for multi-dimensional data storage. We access elements using two indices: one for the outer list and one for the inner list. In this article, we will explain the concept of Lists of Lists in Python, including various
3 min read
Internal working of list in Python
Introduction to Python lists : Python lists are internally represented as arrays. The idea used is similar to implementation of vectors in C++ or ArrayList in Java. The costly operations are inserting and deleting items near the beginning (as everything has to be moved). Insert at the end also becom
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: # import sympy and Point, Line from sympy import Point, Line p1, p
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 Re
2 min read
Python | Extract Combination Mapping in two lists
Sometimes, while working with Python lists, we can have a problem in which we have two lists and require to find the all possible mappings possible in all combinations. This can have possible application in mathematical problems. Let's discuss certain way in which this problem can be solved. Method
2 min read
Convert Two Lists into a Dictionary - Python
We are given two lists, we need to convert both of the list into dictionary. For example we are given two lists a = ["name", "age", "city"], b = ["Geeks", 30,"Delhi"], we need to convert these two list into a form of dictionary so that the output should be like {'name': 'Geeks', 'age': 30, 'city': '
3 min read
Ways to Iterate Tuple List of Lists - Python
In this article we will explore different methods to iterate through a tuple list of lists in Python and flatten the list into a single list. Basically, a tuple list of lists refers to a list where each element is a tuple containing sublists and the goal is to access all elements in a way that combi
3 min read