Open In App

Counting number of unique values in a Python list

Last Updated : 23 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 frequency of each element using a dictionary, finally counting the elements for which the frequency is 1. 

Python3




# function to count the unique elements
def 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 function
if __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. 

Python3




# function to count the unique elements
def 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 function
if __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. 

Python3




# function to count the unique elements
def 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 function
if __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. 

Python3




# importing the module
import collections
 
# function to count the unique elements
def 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 function
if __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) where n is the number of elements in the list
Auxiliary Space: O(1), constant extra space required

 Approach#5: Here we will be using the reduce() method of the functools module to count the frequency. 

Python




from functools import reduce
# function to count the unique elements
def count_unique(my_list):
    # count frequency
    freq = reduce(lambda x,y : x+1 if my_list.count(y)<2 else x, my_list,0)
 
    # displaying the count of unique elements
    print(freq)
 
# driver function
if __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), where n is length of my_list

Auxiliary space : O(n), where n is length of my_list

Approach#6: Using Counter() function

Python3




from collections import Counter
# function to count the unique elements
 
 
def count_unique(my_list):
    freq = Counter(my_list)
    res = 0
    for key, value in freq.items():
        if(value == 1):
            res += 1
    print(res)
 
 
# driver function
if __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)

Auxiliary Space: O(N)

Approach 7:  using operator.countOf() method

Python3




import operator as op
# function to count the unique elements
 
 
def 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] = op.countOf(my_list, x)
 
    # traversing the dictionary
    for key, value in freq.items():
        if value == 1:
            count += 1
 
    # displaying the count of unique elements
    print(count)
 
 
# driver function
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)

Auxiliary Space : O(N)

Approach #8: Using defaultdict()

Here is the algorithm for the count_unique function:

  1. Create an empty defaultdict object unique_dict with default value int.
  2. Iterate through the elements x of the input list my_list.
  3. For each x, increment the value corresponding to x in unique_dict.
  4. Iterate through the values of unique_dict and count the number of values equal to 1.
  5. Print the count as output.

Python3




from collections import defaultdict
 
def count_unique(my_list):
    unique_dict = defaultdict(int)
    for x in my_list:
        unique_dict[x] += 1
    res = sum(1 for x in unique_dict.values() if x == 1)
    print(res)
 
 
if __name__ == "__main__":
    my_list = [10, 20, 10, 30, 40, 40]
    count_unique(my_list)
 
    my_list = ['geeks', 'for', 'geeks']
    count_unique(my_list)
#This code is contributed by Vinay Pinjala.


Output

2
1

Time complexity: O(n) where n is the length of the input list my_list. This is because the function iterates through each element of my_list exactly once.

Auxiliary Space: O(k) where k is the number of unique elements in my_list. This is because the defaultdict unique_dict will have at most k key-value pairs, where k is the number of unique elements in my_list



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads