0
0
PythonProgramBeginner · 2 min read

Python Program to Find Most Common Element in List

Use max(set(lst), key=lst.count) to find the most common element in a list lst in Python.
📋

Examples

Input[1, 2, 2, 3, 3, 3]
Output3
Input["apple", "banana", "apple", "orange", "banana", "apple"]
Outputapple
Input[]
OutputNone
🧠

How to Think About It

To find the most common element, think about counting how many times each item appears in the list. The item with the highest count is the most common. If the list is empty, there is no common element.
📐

Algorithm

1
Check if the list is empty; if yes, return None.
2
Convert the list to a set to get unique elements.
3
For each unique element, count how many times it appears in the original list.
4
Find the element with the highest count.
5
Return that element.
💻

Code

python
def most_common_element(lst):
    if not lst:
        return None
    return max(set(lst), key=lst.count)

# Example usage
print(most_common_element([1, 2, 2, 3, 3, 3]))
Output
3
🔍

Dry Run

Let's trace the list [1, 2, 2, 3, 3, 3] through the code.

1

Check if list is empty

List is [1, 2, 2, 3, 3, 3], not empty, continue.

2

Create set of unique elements

Unique elements are {1, 2, 3}.

3

Count occurrences

Count of 1 is 1, count of 2 is 2, count of 3 is 3.

4

Find max count element

Element with max count is 3.

5

Return result

Return 3.

ElementCount
11
22
33
💡

Why This Works

Step 1: Handle empty list

If the list is empty, return None because there is no element to find.

Step 2: Use set for unique elements

Convert the list to a set to avoid counting the same element multiple times.

Step 3: Find element with highest count

Use max with key=lst.count to find the element that appears most often.

🔄

Alternative Approaches

Using collections.Counter
python
from collections import Counter

def most_common_element(lst):
    if not lst:
        return None
    counter = Counter(lst)
    return counter.most_common(1)[0][0]

print(most_common_element([1, 2, 2, 3, 3, 3]))
This method is faster for large lists and more readable, but requires importing a module.
Using a dictionary to count manually
python
def most_common_element(lst):
    if not lst:
        return None
    counts = {}
    for item in lst:
        counts[item] = counts.get(item, 0) + 1
    max_item = max(counts, key=counts.get)
    return max_item

print(most_common_element([1, 2, 2, 3, 3, 3]))
This method shows how counting works manually without extra modules but is longer.

Complexity: O(n^2) time, O(n) space

Time Complexity

Using max(set(lst), key=lst.count) causes lst.count to run for each unique element, leading to O(n^2) time in the worst case.

Space Complexity

A set of unique elements is created, which takes O(n) space.

Which Approach is Fastest?

Using collections.Counter runs in O(n) time and is faster for large lists compared to the set and count method.

ApproachTimeSpaceBest For
max with set and countO(n^2)O(n)Small lists, simple code
collections.CounterO(n)O(n)Large lists, best performance
Manual dictionary countO(n)O(n)Learning counting logic
💡
Use collections.Counter for an easy and efficient way to find the most common element.
⚠️
Trying to find the most common element without handling empty lists can cause errors.