Counting number of unique values in a Python list

29 Aug, 2020
Let us see how to count the number of unique values in a Python list.Examples :
Input : 10 20 10 30 40 40
Output : 2
Explanation : Only 2 elements, 20 and 30 are unique in the list.

Input : 'geeks' 'for' 'geeks'
Output : 1

Approach 1 : Traversing the list and counting the frequrency of each element using dictionary, finally counting the elements for which the frequency is 1.
# function to count the unique elementsdef count_unique(my_list):      # variable to store the unique count    count = 0      # creating dictionary to count frequency    freq = {}      # traversing the list    for x in my_list:        if (x in freq):            freq[x] += 1        else:            freq[x] = 1      # traversing the dictionary    for key, value in freq.items():        if value == 1:            count += 1      # displaying the count of unique elements    print(count)  # driver functionif __name__ == "__main__":      my_list = [10, 20, 10, 30, 40, 40]    count_unique(my_list)      my_list = ['geeks', 'for', 'geeks']    count_unique(my_list)
Output :
2
1

Time complexity : O(N)Space complexity : O(N)
Approach 2 : Here we will be using the get() method of the dictionary class to count the frequency. This makes the program shorter and demonstrates how get() method is useful instead of if...else.
# function to count the unique elementsdef count_unique(my_list):      # variable to store the unique count    count = 0      # creating dictionary to count frequency    freq = {}      # traversing the list    for x in my_list:        freq[x] = freq.get(x, 0) + 1      # traversing the dictionary    for key, value in freq.items():        if value == 1:            count += 1      # displaying the count of unique elements    print(count)  # driver functionif __name__ == "__main__":      my_list = [10, 20, 10, 30, 40, 40]    count_unique(my_list)      my_list = ['geeks', 'for', 'geeks']    count_unique(my_list)
Output :
2
1

Time complexity : O(N)Space complexity : O(N)
Approach 3 : Here we will be using the count() method of the list class to count the frequency.
# function to count the unique elementsdef count_unique(my_list):      # variable to store the unique count    count = 0      # creating dictionary to count frequency    freq = {}      # traversing the list    for x in my_list:        freq[x] = my_list.count(x)      # traversing the dictionary    for key, value in freq.items():        if value == 1:            count += 1      # displaying the count of unique elements    print(count)  # driver functionif __name__ == "__main__":      my_list = [10, 20, 10, 30, 40, 40]    count_unique(my_list)      my_list = ['geeks', 'for', 'geeks']    count_unique(my_list)
Output :
2
1

Time complexity : O(N)Space complexity : O(N)
Approach 4 : Here we will be using the Counter() method of the collections module to count the frequency.
# importing the moduleimport collections  # function to count the unique elementsdef count_unique(my_list):      # variable to store the unique count    count = 0      # creating dictionary to count frequency    freq = collections.Counter(my_list)      # traversing the dictionary    for key, value in freq.items():        if value == 1:            count += 1      # displaying the count of unique elements    print(count)  # driver functionif __name__ == "__main__":      my_list = [10, 20, 10, 30, 40, 40]    count_unique(my_list)      my_list = ['geeks', 'for', 'geeks']    count_unique(my_list)
Output :
2
1

Python list-programs
python-list
Python
Python Programs
python-list
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Install PIP on Windows ?
Python Classes and Objects
Python OOPs Concepts
Introduction To PYTHON
Python | os.path.join() method
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Convert a list to dictionary
Python Program for Fibonacci numbers
Python | Convert string dictionary to dictionary