Python Program to Find Intersection of Two Lists
You can find the intersection of two lists in Python using
set(list1) & set(list2) which returns common elements as a set, or use a list comprehension like [x for x in list1 if x in list2] to get a list of common items.Examples
Inputlist1 = [1, 2, 3], list2 = [2, 3, 4]
Output[2, 3]
Inputlist1 = ['apple', 'banana'], list2 = ['banana', 'cherry']
Output['banana']
Inputlist1 = [5, 6], list2 = [7, 8]
Output[]
How to Think About It
To find the intersection, think about which items appear in both lists. You can check each item in the first list and see if it also exists in the second list. The items that appear in both are part of the intersection.
Algorithm
1
Get the two input lists.2
Convert both lists to sets to remove duplicates and allow fast lookup.3
Find common elements using the set intersection operator.4
Convert the result back to a list if needed.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
set(list1) & set(list2) = {3, 4}
3
Convert to list
list({3, 4}) = [3, 4]
4
Print 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 quick membership checks.
Step 2: Find common elements
The & operator finds items present in both sets efficiently.
Step 3: Convert back to list
We convert the set back to a list to get a familiar list format for output.
Alternative Approaches
List comprehension
python
list1 = [1, 2, 3, 4] list2 = [3, 4, 5, 6] intersection = [x for x in list1 if x in list2] print(intersection)
This keeps duplicates from list1 and preserves order but is slower for large lists.
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)
Similar to list comprehension but uses filter function; readability depends on preference.
Complexity: O(n + m) time, O(n + m) space
Time Complexity
Converting lists to sets takes O(n) and O(m) time, and intersection is O(min(n, m)) because sets allow fast lookups.
Space Complexity
Extra space is used to store sets of both lists, so O(n + m).
Which Approach is Fastest?
Using sets is fastest for large lists. List comprehension is simpler but slower due to repeated membership checks.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Set intersection | O(n + m) | O(n + m) | Large lists, no duplicates, order not important |
| List comprehension | O(n * m) | O(n) | Small lists, preserve order and duplicates |
| Filter with lambda | O(n * m) | O(n) | Similar to list comprehension, functional style |
Use sets for fast intersection when order and duplicates don't matter.
Trying to find intersection without converting to sets can be slow and may include duplicates.