How to count the number of non-repeating items in a python list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to count the number of non-repeating items in a python list?

For example: a=[1,2,3,3] #there’s two ‘3’ print(len(a)) #it’s not valid 3

22nd Oct 2019, 8:54 PM
Syrex
Syrex - avatar
3 Answers
+ 4
There is a function for that, set() function transforms lists into a set, which removes all repeating values. So: len(ser(a)) = 3
22nd Oct 2019, 8:57 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
Thanks
22nd Oct 2019, 9:10 PM
Syrex
Syrex - avatar
+ 2
A set just removes the duplicate, you want the count of "unique" values (unless i misunderstand your question)..so.. a = [1, 2, 3, 3] counter = 0 for x in a: if a.count(x) == 1: counter += 1 print(counter)
22nd Oct 2019, 10:19 PM
rodwynnejones
rodwynnejones - avatar