Python Program to Find Intersection of Lists
You can find the intersection of two lists in Python by converting them to sets and using the
& operator like intersection = list(set(list1) & set(list2)).Examples
Inputlist1 = [1, 2, 3], list2 = [2, 3, 4]
Output[2, 3]
Inputlist1 = ['apple', 'banana', 'cherry'], list2 = ['banana', 'dragonfruit', 'apple']
Output['apple', 'banana']
Inputlist1 = [5, 6, 7], list2 = [8, 9, 10]
Output[]
How to Think About It
To find the common elements between two lists, think about which items appear in both. Converting lists to sets helps because sets automatically remove duplicates and allow easy comparison using the
& operator to get shared items.Algorithm
1
Take two input lists.2
Convert both lists to sets to remove duplicates and allow set operations.3
Find the intersection of these two sets using the <code>&</code> operator.4
Convert the resulting set back to a list to get the intersection elements.5
Return or print the list of common elements.Code
python
list1 = [1, 2, 3, 4] list2 = [3, 4, 5, 6] intersection = list(set(list1) & set(list2)) print(intersection)
Output
[3, 4]
Dry Run
Let's trace the example where list1 = [1, 2, 3, 4] and list2 = [3, 4, 5, 6] through the code.
1
Convert lists to sets
set(list1) = {1, 2, 3, 4}, set(list2) = {3, 4, 5, 6}
2
Find intersection of sets
set(list1) & set(list2) = {3, 4}
3
Convert intersection set to list
list({3, 4}) = [3, 4]
4
Print the result
Output: [3, 4]
| Operation | Value |
|---|---|
| set(list1) | {1, 2, 3, 4} |
| set(list2) | {3, 4, 5, 6} |
| Intersection | {3, 4} |
| Final list | [3, 4] |
Why This Works
Step 1: Convert lists to sets
Using set() removes duplicates and allows easy comparison with set operations.
Step 2: Find intersection
The & operator finds elements common to both sets efficiently.
Step 3: Convert back to list
We convert the set back to a list to keep the output consistent with the input type.
Alternative Approaches
List comprehension
python
list1 = [1, 2, 3, 4] list2 = [3, 4, 5, 6] intersection = [item for item in list1 if item in list2] print(intersection)
Simple and readable but slower for large lists because it checks membership repeatedly.
Using filter and lambda
python
list1 = [1, 2, 3, 4] list2 = [3, 4, 5, 6] intersection = list(filter(lambda x: x in list2, list1)) print(intersection)
Functional style, similar performance to list comprehension but less common.
Complexity: O(n + m) time, O(n + m) space
Time Complexity
Converting lists to sets takes O(n) and O(m) time, and set intersection is O(min(n, m)), so total is O(n + m).
Space Complexity
Extra space is used to store sets of size n and m, so O(n + m).
Which Approach is Fastest?
Using sets is fastest for large lists because membership checks are O(1), unlike list comprehension which is O(n*m).
| Approach | Time | Space | Best For |
|---|---|---|---|
| Set intersection | O(n + m) | O(n + m) | Large lists, fast performance |
| List comprehension | O(n * m) | O(min(n, m)) | Small lists, simplicity |
| Filter and lambda | O(n * m) | O(min(n, m)) | Functional style, small lists |
Use sets for fast intersection when order and duplicates don't matter.
Trying to find intersection by nested loops without using sets, which is slower and more complex.