I have a list.
l1 = [0, 0, 2, 0]
l2 = [0, 0, 0, 0]
I want to print list if list contains non zero element in it.
Output:
If one one list passed, then only list with non zero element will get printed. In example above only l1 will get printed.
[0, 0, 2, 0]
I want to know how efficiently it can be done. Thanks !
Use any on your lists:
for lst in (l1, l2):
if any(lst):
print(lst)
You can also use all:
for lst in (l1, l2):
if all(x != 0 for x in lst):
print(lst)
I hope this helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With