The Pythonic way is to exploit the fact that zero is interpreted as False in a Boolean context, while all other numbers are considered as True
Comparing to zero - Pythonic way:
>>> bool(0)
False
>>> bool(-1), bool(1), bool(20), bool(28.4)
(True, True, True, True)
Using if item
instead of if item != 0
:
>>> for item in x:
... if item:
... print(item)
...
1
2
3
4
You can also use if not item
instead of if item == 0