Counting value in List | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

Counting value in List

If a list contains False, how can I count value 0 without counting False?

20th Apr 2019, 2:48 PM
Lê Trọng Hoàng Minh
5 Answers
+ 4
False is evaluated as 0, so if you write n==0 False will be removed as well. Instead use: n is 0 False may equal 0, but it is not itself the number 0.
20th Apr 2019, 3:06 PM
HonFu
HonFu - avatar
+ 3
Check this It uses type() to check whether it is integer or not... https://code.sololearn.com/cN19VPZsyqFP/?ref=app
20th Apr 2019, 3:10 PM
Kartik
Kartik - avatar
+ 3
a = [0, 1, False, 0] a.count(0) # 3 len([x for x in a if x is 0]) # 2
20th Apr 2019, 7:08 PM
alex_shchegretsov
alex_shchegretsov - avatar
0
i don’t quite understand your question. can you provide your code so that people can see and provide an answer.
20th Apr 2019, 2:59 PM
Jim
0
thank you guys.
20th Apr 2019, 11:50 PM
Lê Trọng Hoàng Minh