How To Count Unique Values In Python

In this tutorial, we’ll learn how to count unique values in Python using different methods.

Counting unique values is a common task in data analysis and can be useful for understanding the distribution of values in a dataset. We’ll explore some efficient techniques using Python built-in functions, list comprehension, sets, and the Pandas library.

Step 1: Count unique values using a loop and a dictionary

One way to count unique values is by iterating over a list and storing the count of each value in a dictionary. Here’s an example:

You can use this function to count unique values in a list:

Output:

{1: 3, 2: 3, 3: 2, 4: 2, 5: 1}

Step 2: Use list comprehension and count() method

Another approach to count unique values is by using list comprehension and the count() method of the list object. Here’s an example:

This function will return a dictionary with unique values in the list and their respective counts:

Output:

{1: 3, 2: 3, 3: 2, 4: 2, 5: 1}

Step 3: Count unique values using a Counter

Python provides a built-in class called Counter in the collections module which makes it easy to count unique values in a list. Here’s how to use it:

With this function you can count unique values and display them as a dictionary:

Output:

{1: 3, 2: 3, 3: 2, 4: 2, 5: 1}

Step 4: Use Pandas library for counting unique values

You can also use the powerful Pandas library to count unique values in a dataset. Here’s an example with a simple list, but keep in mind that Pandas is particularly useful for larger datasets, such as CSV files or database tables:

Here’s an example of using this function to count unique values in a list:

Output:

{1: 3, 2: 3, 3: 2, 4: 2, 5: 1}

Full code

Here’s the full code for all methods explained in this tutorial:

Conclusion

In this tutorial, we covered various methods to count unique values in Python. You can choose the method that suits you best depending on your requirements and the size of your dataset. Keep in mind, using the Pandas library can be beneficial for complex and large datasets, as it provides additional data processing and analysis tools.