Count distinct elements in an array in Python

In Python lists, we often encounter duplicate elements. While len() gives us the total count including duplicates, we sometimes need to count only the distinct (unique) elements. Python provides several approaches to accomplish this task.

Using Counter from collections

The Counter class from the collections module creates a dictionary where elements are keys and their frequencies are values. We can use its keys() method to get distinct elements ?

from collections import Counter

days = ['Mon', 'Tue', 'Wed', 'Mon', 'Tue']
print("Length of original list:", len(days))

distinct_elements = Counter(days).keys()
print("List with distinct elements:", list(distinct_elements))
print("Length of distinct list:", len(distinct_elements))
Length of original list: 5
List with distinct elements: ['Mon', 'Tue', 'Wed']
Length of distinct list: 3

Using set()

The simplest approach is converting the list to a set, which automatically removes duplicates ?

days = ['Mon', 'Tue', 'Wed', 'Mon', 'Tue']
distinct_count = len(set(days))
print("Count of distinct elements:", distinct_count)
print("Distinct elements:", list(set(days)))
Count of distinct elements: 3
Distinct elements: ['Wed', 'Tue', 'Mon']

Using dict.fromkeys()

This method preserves the original order of first occurrence while removing duplicates ?

days = ['Mon', 'Tue', 'Wed', 'Mon', 'Tue']
distinct_elements = list(dict.fromkeys(days))
print("Distinct elements (order preserved):", distinct_elements)
print("Count of distinct elements:", len(distinct_elements))
Distinct elements (order preserved): ['Mon', 'Tue', 'Wed']
Count of distinct elements: 3

Comparison

Method Preserves Order Performance Best For
set() No Fastest Simple counting
dict.fromkeys() Yes Fast Order matters
Counter No Slower Need frequency info

Conclusion

Use len(set(list)) for simple distinct counting. Use dict.fromkeys() when order preservation is important. Use Counter when you need both distinct elements and their frequencies.

Updated on: 2026-03-15T17:01:32+05:30

678 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements