Counting number of unique values in a Python list
Last Updated :
23 Apr, 2023
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
def count_unique(my_list):
count = 0
freq = {}
for x in my_list:
if (x in freq):
freq[x] + = 1
else :
freq[x] = 1
for key, value in freq.items():
if value = = 1 :
count + = 1
print (count)
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
def count_unique(my_list):
count = 0
freq = {}
for x in my_list:
freq[x] = freq.get(x, 0 ) + 1
for key, value in freq.items():
if value = = 1 :
count + = 1
print (count)
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
def count_unique(my_list):
count = 0
freq = {}
for x in my_list:
freq[x] = my_list.count(x)
for key, value in freq.items():
if value = = 1 :
count + = 1
print (count)
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
import collections
def count_unique(my_list):
count = 0
freq = collections.Counter(my_list)
for key, value in freq.items():
if value = = 1 :
count + = 1
print (count)
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
def count_unique(my_list):
freq = reduce ( lambda x,y : x + 1 if my_list.count(y)< 2 else x, my_list, 0 )
print (freq)
if __name__ = = "__main__" :
my_list = [ 10 , 20 , 10 , 30 , 40 , 40 ]
count_unique(my_list)
my_list = [ 'geeks' , 'for' , 'geeks' ]
count_unique(my_list)
|
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
def count_unique(my_list):
freq = Counter(my_list)
res = 0
for key, value in freq.items():
if (value = = 1 ):
res + = 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)
|
Time Complexity: O(N)
Auxiliary Space: O(N)
Approach 7: using operator.countOf() method
Python3
import operator as op
def count_unique(my_list):
count = 0
freq = {}
for x in my_list:
freq[x] = op.countOf(my_list, x)
for key, value in freq.items():
if value = = 1 :
count + = 1
print (count)
my_list = [ 10 , 20 , 10 , 30 , 40 , 40 ]
count_unique(my_list)
my_list = [ 'geeks' , 'for' , 'geeks' ]
count_unique(my_list)
|
Time Complexity: O(N)
Auxiliary Space : O(N)
Approach #8: Using defaultdict()
Here is the algorithm for the count_unique function:
- Create an empty defaultdict object unique_dict with default value int.
- Iterate through the elements x of the input list my_list.
- For each x, increment the value corresponding to x in unique_dict.
- Iterate through the values of unique_dict and count the number of values equal to 1.
- 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)
|
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
Share your thoughts in the comments
Please Login to comment...