Write a Python Program to count unique values inside a list


The program is counting the number of unique items in a given list. It first initializes an empty list called "l" and a count variable set to 0. Then, the program uses a for loop to iterate through the list "a". For each item in the list, it checks if that item is already in the list "l". If it is not, the count variable is incremented by 1, and the item is added to the list "l". This process continues for all items in the list "a", and at the end, the program prints out the final count of unique items in the list "a". This is done by checking if the element is already present in the l list or not. If not found then we increment the count variable and add the element in the list l. At the end we print the count which gives the number of unique elements in the list a.

Source Code

"""
#First Method
 
a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
s = set(a)
print("No of Unique Items in List :", len(s))
"""
#Second Method
 
a = [10, 20, 30, 50, 80, 70, 70, 80, 10]
l = []
count = 0
for i in a:
	if i not in l:
		count += 1
		l.append(i)
 
print("No of Unique Items in List :", count)

Output

No of Unique Items in List : 6



Example Programs