0
0
PythonProgramBeginner · 2 min read

Python Program to Check if Two Lists Have Common Elements

You can check if two lists have common elements in Python by using bool(set(list1) & set(list2)), which returns True if there is any overlap and False otherwise.
📋

Examples

Inputlist1 = [1, 2, 3], list2 = [3, 4, 5]
OutputTrue
Inputlist1 = ['apple', 'banana'], list2 = ['cherry', 'date']
OutputFalse
Inputlist1 = [], list2 = [1, 2, 3]
OutputFalse
🧠

How to Think About It

To find if two lists share any common elements, think about comparing each item from the first list with items in the second list. If you find any item that appears in both, you know they have something in common. Using sets helps because sets automatically remove duplicates and allow quick checks for shared items using the intersection operation.
📐

Algorithm

1
Convert both lists into sets to remove duplicates and allow fast operations.
2
Find the intersection of these two sets to get common elements.
3
Check if the intersection set is not empty.
4
Return True if there are common elements, otherwise return False.
💻

Code

python
def have_common_elements(list1, list2):
    return bool(set(list1) & set(list2))

# Example usage
list1 = [1, 2, 3]
list2 = [3, 4, 5]
print(have_common_elements(list1, list2))
Output
True
🔍

Dry Run

Let's trace the example where list1 = [1, 2, 3] and list2 = [3, 4, 5] through the code.

1

Convert lists to sets

set(list1) = {1, 2, 3}, set(list2) = {3, 4, 5}

2

Find intersection

set(list1) & set(list2) = {3}

3

Check if intersection is not empty

bool({3}) = True

4

Return result

Function returns True

OperationValue
set(list1){1, 2, 3}
set(list2){3, 4, 5}
Intersection{3}
ResultTrue
💡

Why This Works

Step 1: Convert lists to sets

Using set() removes duplicates and allows fast operations like intersection.

Step 2: Find intersection

The & operator finds common elements between two sets.

Step 3: Check if intersection is not empty

If the intersection set has any elements, bool() returns True, meaning lists share common items.

🔄

Alternative Approaches

Using a loop and membership check
python
def have_common_elements(list1, list2):
    for item in list1:
        if item in list2:
            return True
    return False

print(have_common_elements([1, 2, 3], [3, 4, 5]))
Simple to understand but slower for large lists because it checks each item one by one.
Using set.isdisjoint() method
python
def have_common_elements(list1, list2):
    return not set(list1).isdisjoint(set(list2))

print(have_common_elements([1, 2, 3], [3, 4, 5]))
More readable and efficient; <code>isdisjoint()</code> returns True if sets have no common elements.

Complexity: O(n + m) time, O(n + m) space

Time Complexity

Converting lists to sets takes O(n) and O(m) time, where n and m are list lengths. Intersection operation is O(min(n, m)). Overall, this is efficient for large lists.

Space Complexity

Extra space is used to store sets of size up to n and m, so O(n + m).

Which Approach is Fastest?

Using sets and intersection is faster than looping through lists because set operations are optimized for membership checks.

ApproachTimeSpaceBest For
Set intersectionO(n + m)O(n + m)Large lists, fast checks
Loop with membershipO(n * m)O(1)Small lists, simple code
Set.isdisjoint()O(n + m)O(n + m)Readable and efficient
💡
Convert lists to sets first to speed up checking for common elements.
⚠️
Checking membership with nested loops without sets can be very slow for large lists.