Count Unique Values in List in Python (4 Examples)

 

In this tutorial, we will show how to count unique values in a list in the Python programming language.

The table of content is structured as follows:

Let’s dive right in!

 

Example Data

First, we will define some example data for this tutorial.

my_list = [5, 2, 3, 3, 8, 10, 
           9, 4, 5, 6, 6, 7]
print(my_list)
# [5, 2, 3, 3, 8, 10, 
#  9, 4, 5, 6, 6, 7]

The previous output shows that our example data is a list object named my_list that contains 12 integers: some of them are unique values, and some others are repeated. Let’s see in the following examples how to count only the unique values in our list.

 

Example 1: Count Number of Unique Items in List Using set() Function

In this example, we will create a set object using our list named unique. Set objects only contain unique elements, so printing the length of our set using the len() function will give us the total number of unique elements.

unique = set(my_list)
len(unique)
# 9

As shown, there are nine unique elements in my_list.

 

Example 2: Count Number of Unique Items in List Using Counter Class & keys() Function

A way to get the unique elements in a list is to use the Counter class of the collections module. To use it, first, we will import it.

from collections import Counter

Next, we will plug my_list into the Counter() constructor, which will create a dictionary with the values in my_list and the times each value appears in it. We will then keep the keys in a dict_keys object named unique. Its length will show us the number of unique values in my_list.

unique = Counter(my_list).keys()
len(unique)
# 9

We can see that the result matches the one we obtained in the first example: my_list contains nine unique values.

 

Example 3: Count Number of Unique Items in List Using List Comprehension

An alternative to counting the unique elements in a list is using the list comprehension method. This will iterate through the enumerated elements of the list and keep the unique values by the if statement x not in my_list[:i]. Again, printing the length of this new list named unique will give us the number of unique elements in my_list.

unique = [ x for i, x in enumerate(my_list) if x not in my_list[:i]]
len(unique)
# 9

Nice! The result is correct: nine items in our list are unique.

 

Example 4: Count Number of Unique Items in List Using For Loop

This example shows how to count the number of unique items in a list using a for loop, a count variable, and an empty array. Take a look at the code below.

unique = []
counter = 0
for item in my_list:
    if item not in unique:
        counter += 1
        unique.append(item)
print(counter)
# 9

As shown in this example we iterate through my_list and add the element to the empty list named unique if it’s not in it yet. Each time when a value is added to the unique list, the counter increases by 1. The result is 9, just as expected!

 

Video, Further Resources & Summary

Do you need more explanations on how to count the unique elements in a list in Python? Then you should have a look at the following YouTube video of the Statistics Globe YouTube channel.

 

The YouTube video will be added soon.

 

In addition, you may want to have a look at the related programming tutorials on Statistics Globe:

This post has shown how to count the number of unique elements in a list in Python. Please let me know in the comments section below in case you have any additional questions.

 

Paula Villasante Soriano Statistician & R Programmer

This page was created in collaboration with Paula Villasante Soriano. Please have a look at Paula’s author page to get further information about her academic background and the other articles she has written for Statistics Globe.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.

Top