Python Program to Find Intersection of Two Lists
The intersection of two lists is elements that appear on both lists. There are multiple ways to obtain the intersection of two lists using Python, like using loops, set operations, and list comprehensions.
Methods to Get Intersection of Two Lists
1. Using Loop
This uses a loop in order to compare each element in one list to the other list.
2. Using List Comprehension
A more elegant way of expressing the same as above using list comprehension in Python.
3. Using set.intersection() Method
Using Python’s set data type to find intersection in an efficient manner.
4. Using filter() and lambda
Functional Programming using filter() and lambda.
Method 1: Loop
- Create two lists.
- Initialize an empty list to store the intersection.
- Iterate through the first list.
- If an element exists in the second list and is not already in the result, add it.
- Print the final result.
# Define two lists
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
# Empty list to store intersection
intersection = []
# Loop through the first list
for item in list1:
if item in list2 and item not in intersection:
intersection.append(item)
# Print the intersection
print("Intersection:", intersection)
Output:
Intersection: [4, 5, 6]
Time Complexity
- O(n * m) (where
nandmare lengths of the two lists) because for each element inlist1, we check if it’s inlist2.
Method 2: Using List Comprehension
- This method is similar to the loop method but more concise.
- We use list comprehension to filter common elements.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
# List comprehension to find intersection
intersection = [item for item in list1 if item in list2]
print("Intersection:", intersection)
Output:
Intersection: [4, 5, 6]
Time Complexity
- O(n * m), since for each item in
list1, it checks if it exists inlist2.
Method 3: Using set.intersection()
- Convert both lists to sets.
- Use the
.intersection()method to find common elements. - Convert the result back to a list.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
# Convert to sets and find intersection
intersection = list(set(list1).intersection(set(list2)))
print("Intersection:", intersection)
Output:
Intersection: [4, 5, 6]
Time Complexity
- O(n + m) (faster than previous methods) because:
- Converting lists to sets takes O(n + m).
- Finding the intersection takes O(min(n, m)).
Method 4: Using filter() and lambda
- The
filter()function filters elements based on a condition. - The lambda function checks if an element from
list1is inlist2.
list1 = [1, 2, 3, 4, 5, 6]
list2 = [4, 5, 6, 7, 8, 9]
# Using filter() and lambda
intersection = list(filter(lambda x: x in list2, list1))
print("Intersection:", intersection)
Output:
Intersection: [4, 5, 6]
Time Complexity
- O(n * m) because
lambda x: x in list2checks for each element inlist1whether it exists inlist2.
Best Method?
| Method | Code Simplicity | Performance |
|---|---|---|
| Loop | Easy | Slow (O(n*m)) |
| List Comprehension | Concise | Slow (O(n*m)) |
set.intersection() | Best | Fastest (O(n+m)) |
filter() + lambda | Functional Style | Slow (O(n*m)) |
Conclusion
- For large lists, Method 3 (Set Intersection) is the best as it has the fastest performance (
O(n + m)). - If lists are small, any method works fine.