Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python - Intersection of multiple lists
In this article, we will explore different approaches to find the intersection of multiple lists containing sub-lists. The intersection returns only the common sub-lists present in all input lists.
Using List Comprehension
The simplest approach uses list comprehension to check if each sub-list exists in all other lists ?
# initializing the lists
list_1 = [[1, 2], [3, 4], [5, 6]]
list_2 = [[3, 4], [7, 8]]
# finding the common items from both lists
result = [sub_list for sub_list in list_1 if sub_list in list_2]
# printing the result
print("Intersection using list comprehension:", result)
Intersection using list comprehension: [[3, 4]]
Using Set Intersection
Convert sub-lists to tuples (since lists are not hashable) and use set intersection for better performance ?
# initializing the lists
list_1 = [[1, 2], [3, 4], [5, 6]]
list_2 = [[3, 4], [7, 8]]
# converting each sub list to tuple for set support
tuple_1 = map(tuple, list_1)
tuple_2 = map(tuple, list_2)
# intersection using sets
result = list(map(list, set(tuple_1).intersection(tuple_2)))
# printing the result
print("Intersection using sets:", result)
Intersection using sets: [[3, 4]]
Intersection of Multiple Lists
For finding intersection among more than two lists, use the & operator or intersection() method ?
# initializing multiple lists
list_1 = [[1, 2], [3, 4], [5, 6]]
list_2 = [[3, 4], [5, 6]]
list_3 = [[3, 4], [9, 10]]
# convert to sets of tuples
set_1 = set(map(tuple, list_1))
set_2 = set(map(tuple, list_2))
set_3 = set(map(tuple, list_3))
# find intersection of all three sets
result = list(map(list, set_1 & set_2 & set_3))
print("Intersection of three lists:", result)
Intersection of three lists: [[3, 4]]
Using reduce() for Dynamic Lists
When dealing with a variable number of lists, use functools.reduce() ?
from functools import reduce
# list of lists to intersect
all_lists = [
[[1, 2], [3, 4], [5, 6]],
[[3, 4], [5, 6], [7, 8]],
[[3, 4], [9, 10]]
]
# convert each list to set of tuples and find intersection
sets = [set(map(tuple, lst)) for lst in all_lists]
result = list(map(list, reduce(set.intersection, sets)))
print("Dynamic intersection:", result)
Dynamic intersection: [[3, 4]]
Comparison
| Method | Time Complexity | Best For |
|---|---|---|
| List Comprehension | O(n²) | Small lists, simple cases |
| Set Intersection | O(n) | Large lists, better performance |
| reduce() | O(n) | Variable number of lists |
Conclusion
Use set intersection for better performance with large datasets. List comprehension works well for simple cases, while reduce() handles dynamic scenarios with multiple lists efficiently.
