How to count unique values inside a list

There are several methods for finding or counting unique items inside a list in Python. Here we’ll discuss 3 methods.

Method 1: 

The first method is the brute force approach. This method is not very much efficient as it takes more time and space. In this method, we take an empty array and a count variable(set to be zero). We traverse from the start and check the items. If the item is not in the empty list(as it has taken empty) then we will add it to the empty list and increase the counter by 1. While traveling if the item is in the taken list(empty list) we will not count it. 

Example:

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# taking an input list
input_list = [1, 2, 2, 5, 8, 4, 4, 8]
  
# taking an input list
l1 = []
  
# taking an counter
count = 0
  
# travesing the array
for item in input_list:
    if item not in l1:
        count += 1
        l1.append(item)
  
# printing the output
print("No of unique items are:", count)

chevron_right


Output:



No of unique items are: 5

Method 2:

In this method, we will use a function name Counter. The module collections have this function. Using the Counter function we will create a dictionary. The keys of the dictionary will be the unique items and the values will be the number of that key present in the list.  We will create a list using the keys, the length of the list will be our answer.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# importing Counter module
from collections import Counter
  
  
input_list = [1, 2, 2, 5, 8, 4, 4, 8]
  
# creating a list with the keys
items = Counter(input_list).keys()
print("No of unique items in the list are:", len(items))

chevron_right


Output:

No of unique items in the list are: 5

If we print the length of the dictionary created using Counter will also give us the result. But this method is more understandable.

Method 3:

In this method, we will convert our list to set. As sets don’t contain any duplicate items then printing the length of the set will give us the total number of unique items.

Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

input_list = [1, 2, 2, 5, 8, 4, 4, 8]
  
# converting our list to set
new_set = set(input_list)
print("No of unique items in the list are:", len(new_set))

chevron_right


Output:

No of unique items in the list are: 5

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Hi, I am Santanu, currently pursuing my BTech degree in Electrical Engineering I love to code I usually do code in Python I am also a Web Content Writer I post articles in GFG portal My user handle is santanunandi01, give a google search and you will find my other social media profiles

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.