Counting nonzero elements in a list with Python list comprehensions
- by AntiRush
I have a list of integers and I need to count how many of them are 0.
I'm currently doing it with a list comprehension that looks like this:
sum([1 for x in frequencies if x > 0])
It seems like a decent comprehension but I don't really like the "1"; it seems like a bit of a magic number. Is there a more Pythonish way to do this?