8 Things To Know To Count Unique Values in a List Using Python

How to count unique values in a Python list? There are multiple ways and in this tutorial we will go through them as a way for you to practice several Python constructs.

The simplest way to count unique values in a Python list is to convert the list to a set considering that all the elements of a set are unique. You can also count unique values in a list using a dictionary, the collections.Counter class, Numpy.unique() or Pandas.unique().

Most of our examples will work on the same list of numbers:

We will implement every approach in its own function so it’s easy to call them from the “main” of our code. Every function takes the numbers list as input and returns the list with unique values.

The main of our program will then count the number of unique values using the list len() method.

Let’s get started!

1. Using a For Loop to Count Unique Values in a Python List

The first approach uses a Python for loop to go through all the elements.

    for value in values:
if value not in unique_values:
return unique_values

The for loop goes through every element of the list and adds that element to a new list if it’s not already present in the new list.

Let’s write a generic code to print the result returned by this function so we can use pretty much the same code to print the result returned by the other functions we will create in the next sections.

The output is:

The only line we will have to update when calling a different function is the following:

Everything else in the main of our program will stay the same because it depends on the variable unique_values.

2. Count Unique Values in a Python List Using a Set

The set is a data structure that only contains unique elements. We can use this property of the set to get back just unique values.

unique_values = get_unique_values_with_set(numbers)

Notice the curly brackets in the output due to the fact that the function returns a set:

Do you see that the order of the elements has changed compared to the original list?

That’s because…

The set data type is unordered.

The nice thing of this approach is that the code is a lot more concise (just a single line function).

3. Using a Dictionary to Get the Number of Unique Values in a List

Could we use the fact that dictionary keys are unique to get unique elements from the original list?

We could create a dictionary where the unique elements in the list are the keys of the dictionary. The values of the dictionary are counters that tell us how many time each element appears in the list.

    for value in values:
if value in new_values.keys():
new_values[value] += 1
new_values[value] = 1

return new_values.keys()
unique_values = get_unique_values_with_dictionary(numbers)

At each iteration of the for loop we verify if a specific value already exists as dictionary key. If it doesn’t we add that key to the dictionary and we set the value to 1.

Otherwise we increase the value associated to the existing key in the dictionary.

I have added a print() statement in the function to show you the dictionary created.

4. Collections.Counter To Count Unique Values in a List

The same approach explained in the previous section (using a standard dictionary) can also be implemented using the Counter class of the Collections module.

Let’s find out what Counter does exactly:


It generates a dictionary-like data structure where the keys are the elements of the list and the values indicate the count of each element in the original list.

Basically the same thing we have created in the previous section using a standard Python dictionary.

Let’s apply it to our list of numbers:

These are the methods available for the Counter object:

The .keys() method is the one we need to get the unique elements in the original list:

So, we can add the following function to our code:

def get_unique_values_with_counter(values):
return Counter(values).keys()
unique_values = get_unique_values_with_counter(numbers)

The output is:

In the same way we have seen when using the set, with this approach we can write a one-line function.

5. Counting Unique Values in a List Using NumPy

The NumPy library also provides a way to count unique values in a list (or array).

NumPy.unique not only allows to find the unique elements of an array but it also provides the number of times each element is present by using the return_counts parameter.

So, let’s create a new function that uses np.unique():

...def get_unique_values_with_numpy(values):
return np.unique(values)
unique_values = get_unique_values_with_numpy(numbers)

And here are the unique elements we get back:

6. Using Pandas to Get Unique Values in a List

We can also get unique values in a list using Pandas. To do that we will use pandas.unique.

It takes as input a one-dimensional array, so let’s write our function:

...def get_unique_values_with_pandas(values):
return pd.unique(pd.Series(values))
unique_values = get_unique_values_with_pandas(numbers)

The output returned by our program is:

7. Count Unique Values in a List of Dictionaries

Let’s write code to do something a bit more complex.

I have a list of dictionaries that all contain the same key and I want to get the count of unique values across all the dictionaries in the list.

The first step would be to get a list that contains all the values in each dictionary. To do that we can use a list comprehension.

Now we can use one of the approaches already used before. For example we can pass the list to collections.Counter:

Makes sense?

8. Which One is the Faster Approach to Count Unique List Values?

We will complete this tutorial by comparing the performance of all the approaches we have implemented to solve our problem.

To do that we will generate a list with random elements and then use the timeit module to measure the execution time for each implementation.

We will generate the list of random numbers using the Python random module:

This is just an example of list that contains 10 random numbers between 0 and 9. To measure the performance of our function we will use a bigger list with random numbers in a wider range.

Let’s take the first function we have created, the one that uses the for loop. Here is how we can pass it to the timeit.timeit function:

testfunction = '''
def get_unique_values_with_for_loop(values):
unique_values = []
for value in values:
if value not in unique_values:
return unique_values
random_numbers = [random.randrange(100) for i in range(1000000)]
print(timeit.timeit(testfunction, number=100000000))

The number parameter represents the number of executions.

The output is:

To measure the execution time for every function with timeit simply surround a given function with the following two lines in the same way we have done above:

Let’s compare the execution time between the six approaches implemented in this tutorial:

The fastest implementation is the first one that uses a Python for loop and most of them have a comparable performance.


After reading this article you have lots of different ways to count unique values in a Python list.

I hope you have found this tutorial useful to see how you can solve the same problem in many different ways using Python.

Which one of the solutions we have implemented do you prefer? :)

Originally published at https://codefather.tech on April 8, 2021.

I’m a Tech Lead, Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store