0
0
PythonProgramBeginner · 2 min read

Python Program to Find Common Elements in Two Lists

You can find common elements in two lists using set(list1) & set(list2) which returns the intersection as a set, or use a list comprehension like [x for x in list1 if x in list2].
📋

Examples

Input[1, 2, 3], [2, 3, 4]
Output[2, 3]
Input["apple", "banana", "cherry"], ["banana", "dragonfruit", "apple"]
Output["apple", "banana"]
Input[5, 6, 7], [8, 9, 10]
Output[]
🧠

How to Think About It

To find common elements, think of comparing each item in the first list with items in the second list. If an item appears in both, it is common. Using sets helps because they automatically remove duplicates and allow quick comparison with the & operator.
📐

Algorithm

1
Take two input lists.
2
Convert both lists to sets to remove duplicates and allow fast lookup.
3
Find the intersection of these two sets to get common elements.
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]
common_elements = list(set(list1) & set(list2))
print(common_elements)
Output
[3, 4]
🔍

Dry Run

Let's trace the example lists [1, 2, 3, 4] and [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

common_elements = [3, 4]

4

Print result

Output: [3, 4]

OperationValue
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 fast comparison using set operations.

Step 2: Find intersection

The & operator finds elements present in both sets, which are the common elements.

Step 3: Convert back to list

We convert the set back to a list to have a standard list output that can be used or printed.

🔄

Alternative Approaches

List comprehension
python
list1 = [1, 2, 3, 4]
list2 = [3, 4, 5, 6]
common_elements = [x for x in list1 if x in list2]
print(common_elements)
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]
common_elements = list(filter(lambda x: x in list2, list1))
print(common_elements)
Similar to list comprehension but uses filter function; also slower for large lists.

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 overall 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 due to O(1) average lookup, while list comprehension is slower with O(n*m) time.

ApproachTimeSpaceBest For
Set IntersectionO(n + m)O(n + m)Large lists, fast lookup
List ComprehensionO(n * m)O(n)Small lists, simplicity
Filter with LambdaO(n * m)O(n)Small lists, functional style
💡
Use sets for faster common element search when order and duplicates don't matter.
⚠️
Trying to find common elements without converting to sets can be slow and may include duplicates.