Element with largest frequency in list in Python

Finding the element with the largest frequency in a list is a common task in data analysis and statistics. Python provides several built-in approaches to accomplish this efficiently using the collections.Counter class and the statistics.mode function.

Using Counter from collections

The Counter class provides a most_common() method that returns elements with their frequencies in descending order. You can pass a parameter to limit the number of results ?

Example

from collections import Counter

# Given list
days_and_numbers = ['Mon', 'Tue', 'Mon', 9, 3, 3]

print("Given list:", days_and_numbers)

# Find the single most common element
most_common_one = Counter(days_and_numbers).most_common(1)
most_common_two = Counter(days_and_numbers).most_common(2)

# Results
print("Most common element:", most_common_one)
print("Top 2 most common elements:", most_common_two)
Given list: ['Mon', 'Tue', 'Mon', 9, 3, 3]
Most common element: [('Mon', 2)]
Top 2 most common elements: [('Mon', 2), (3, 2)]

The most_common() method returns a list of tuples where each tuple contains the element and its frequency count.

Using mode from statistics

The mode() function returns the single most frequent element directly. If multiple elements have the same highest frequency, it returns the first one encountered ?

Example

from statistics import mode

# Given lists
listA = ['Mon', 'Tue', 'Mon', 9, 3, 3]
listB = [3, 3, 'Mon', 'Tue', 'Mon', 9]

print("Given listA:", listA)
print("Given listB:", listB)

# Find mode for each list
mode_A = mode(listA)
mode_B = mode(listB)

# Results
print("Mode of listA:", mode_A)
print("Mode of listB:", mode_B)
Given listA: ['Mon', 'Tue', 'Mon', 9, 3, 3]
Given listB: [3, 3, 'Mon', 'Tue', 'Mon', 9]
Mode of listA: Mon
Mode of listB: 3

Notice that when multiple elements have the same frequency, mode() returns the first one encountered in the original order.

Comparison

Method Return Type Multiple Results Best For
Counter.most_common() List of tuples Yes When you need frequency counts
statistics.mode() Single element No When you need just the element

Conclusion

Use Counter.most_common() when you need frequency information or multiple results. Use statistics.mode() for a simple, direct answer when you only need the most frequent element.

Updated on: 2026-03-15T17:47:39+05:30

608 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements