# Count Unique Values in Python List

In this article, we will learn how to count unique values present in a list in Python. We will use some built-in functions, a simple approach, and some custom codes as well. Let's first have a quick look over what is a list in Python and about unique values in a list in this article.

## Python List

Python has a built-in data type called list. It is like a collection of arrays with different methodology. Data inside the list can be of any type say, integer, string or a float value, or even a list type. The list uses comma-separated values within square brackets to store data. Lists can be defined using any variable name and then assigning different values to the list in a square bracket. The list is ordered, changeable, and allows duplicate values.

``````list1 = [‘Ram’,’Arun’,‘Kiran’]
list2 = [16,78,32,67]
list3 = [‘apple’,’mango’,16,’cherry’,3.4]``````

A program will ask the user for input and stores the values in an array/list. Then a blank line is entered, it will tell the user how many of the values are unique. The program may have duplicate elements as well. When we count the length of the list we get the total length including the duplicate elements. But in this article, we will see how to get the total count of the distinct elements or unique elements in a list.

## Find Unique Elements

The number of unique values in a list is the number of elements excluding duplicates. For example, the number of unique values in [1, 1, 2, 2, 3] is 3.

Let us look at the below ways and introduce different methods to count unique values inside a list. This article uses the following methods:

1. Brute Force Approach
2. Using Collections module
3. Using set() function
4. Using NumPy function

## Example: Brute Force Approach to Find Unique Elements

The first method is the `Brute Force Approach`. This technique is not productive as it requires some investment and space. The brute force approach uses a simple algorithm to count the unique values and it does not require any function to calculate unique values. This method takes an empty list and a count variable to count the unique values. We navigate from the beginning and check each element. If that element is not present in the empty list, then we add that element to the list and increment the counter by 1. While traversing, if that element is present in the empty list, we will not increase the counter variable and will move to the next iteration.

Let us understand this with the help of an example given below.

``````#input list
input_list = [1, 2, 2, 5, 8, 4, 4, 8]

#empty list to store unique values
empty_list = []

#counter variable to count unique values
count = 0

# travesing the array
for ele in input_list:
if(ele not in empty_list):
count += 1
empty_list.append(ele)

#output
print("Count of unique values are:", count)``````

Count of unique values are: 5

## Example: Using Collections module to Find Unique Elements

This method will use `collections.counter()` function to count the unique values in the given Python list. In this method, a function named counter() is imported from the collections module.

`collections`- It is a Python standard library, and it contains the counter class to count the hashable items.

`counter`- Counter function is used to create a dictionary Counter is a` dict` subclass for enumerating hashable items. Counter is an unordered collection where values are stored as dictionary keys and the count values are stored as dictionary values. It has two methods :

• `keys()` - returns the unique values in the list.

• `values() `- returns the count of every unique value in the list.

After importing the counter function from collections, we declare an input list. From this input list, we create another list made up of only the items whose key values are present once. This list is a distinct list of items. We know counter prints data in the form of a dictionary. So, the keys of the dictionary will be the unique items (use `counter.keys() `function) and the values will be the number of that key present in the list (use `counter.values()` function). As a result, we find the length of this new list. We use the `len()` function to get the number of unique values by passing the counter class as the argument.

Let us understand this with the help of an example given below.

``````#import Counter
from collections import Counter

#input list
input_list = ['X', 'B', 'G', 'X','G']

#keys of the dictionary will be the unique items
print(Counter(input_list).keys())

#values will be the number of that key present in the list
print(Counter(input_list).values())

#new list with key-value pairs
print(Counter(input_list))

#count of unique values
print("Count- ", len(Counter(input_list)))``````

dict_keys(['X', 'B', 'G'])
dict_values([2, 1, 2])
Counter({'X': 2, 'B': 1, 'G': 2})
Count- 3

## Example: Using set() function to Find Unique Elements

In this method, we use `set()` and` len()` function to calculate the number of unique items present in the list. Set is another data type available in Python just like lists. Set is different from a list because set is an unordered data type while the list is an ordered data type and most importantly set does not allow repeated or duplicate elements while the list allows duplicate elements. We can get the length of the set to count unique values using len() function in the list after we convert the list to a set using the set() function.

``````#input list
input_list = [1, 4, 4, 1, 9]

#converts list to set
to_set = set(input_list)

#prints count of unique values
print("Count- ", len(to_set))``````

Count- 3

## Example: Using NumPy module to Find Unique Elements

This example uses a built-in NumPy function called `numpy.unique() `to count unique values. `numpy.unique()` function returns the unique values and takes array-like data as an input list. It also returns the count of each unique element if the `return_counts` parameter is set to be True.

Let us look at the below example to understand this function.

``````#numpy is imported
import numpy as np

#input list
input_list = [4, 6, 2, 6, 7, 4, 2, 2, 0]

#prints unique elements
print("Unique elements are- ",np.unique(input_list))

#prints total number of unique elements
print("Count of unique elements- ",len(np.unique(input_list)))``````

Unique elements are- [0 2 4 6 7]
Count of unique elements- 5

### Conclusion

In this article, we learned how to count unique elements present in a list by using several built-in functions such as `Counter()`, `set()`, `len()`, `numpy.unique()` etc and we used some custom examples as well to understand the working of functions. We learned about the difference between set data type and list data type and how the list is converted to set using the set() function. We can print the count of unique elements as an integer as well as in the form of a dictionary with key-value pairs as discussed above in the article.