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
Find common elements in list of lists in Python
When working with nested lists, you may need to find elements that appear in all inner lists. Python provides several approaches to find common elements across multiple lists using set operations.
Using map() and set.intersection()
The most straightforward approach uses set intersection. We convert each inner list to a set and find their intersection ?
nested_lists = [['Mon', 3, 'Tue', 7, 'Wed', 4],
['Thu', 5, 'Fri', 11, 'Tue', 7],
['Wed', 9, 'Tue', 7, 'Wed', 6]]
print("Given list of lists:")
print(nested_lists)
# Convert each list to set and find intersection
common_elements = list(set.intersection(*map(set, nested_lists)))
print("The common elements among inner lists:", common_elements)
Given list of lists: [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inner lists: ['Tue', 7]
Using reduce() and Lambda Function
The reduce() function applies a function cumulatively to items in a sequence. We use the set intersection operator & with lambda ?
from functools import reduce
nested_lists = [['Mon', 3, 'Tue', 7, 'Wed', 4],
['Thu', 5, 'Fri', 11, 'Tue', 7],
['Wed', 9, 'Tue', 7, 'Wed', 6]]
print("Given list of lists:")
print(nested_lists)
# Using reduce with lambda for intersection
common_elements = list(reduce(lambda i, j: i & j, (set(n) for n in nested_lists)))
print("The common elements among inner lists:", common_elements)
Given list of lists: [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inner lists: ['Tue', 7]
Using Loop-Based Approach
For better readability, you can use a simple loop to find common elements ?
nested_lists = [['Mon', 3, 'Tue', 7, 'Wed', 4],
['Thu', 5, 'Fri', 11, 'Tue', 7],
['Wed', 9, 'Tue', 7, 'Wed', 6]]
print("Given list of lists:")
print(nested_lists)
# Start with the first list as a set
common_elements = set(nested_lists[0])
# Intersect with each subsequent list
for inner_list in nested_lists[1:]:
common_elements = common_elements.intersection(set(inner_list))
print("The common elements among inner lists:", list(common_elements))
Given list of lists: [['Mon', 3, 'Tue', 7, 'Wed', 4], ['Thu', 5, 'Fri', 11, 'Tue', 7], ['Wed', 9, 'Tue', 7, 'Wed', 6]] The common elements among inner lists: ['Tue', 7]
Comparison of Methods
| Method | Readability | Performance | Best For |
|---|---|---|---|
set.intersection(*map(set, lists)) |
Moderate | Fastest | Concise one-liner |
reduce(lambda i, j: i & j, ...) |
Low | Good | Functional programming |
| Loop-based approach | High | Good | Clear and readable code |
Conclusion
Use set.intersection(*map(set, lists)) for the most concise solution. The loop-based approach offers better readability for complex scenarios. All methods efficiently handle finding common elements across nested lists.
