Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python: using "if not" on multiple items

Can I do this in Python:

if not (list1, list2, list3):
    ...

To check if all given lists are empty?

If not how else would I do it?

like image 735
Rotareti Avatar asked Mar 22 '16 20:03

Rotareti


Video Answer


1 Answers

A tuple that has at least one element is truthy in boolean context. This means that not (list1, list2, list3) is always False.

Since empty lists are falsy, you can use the built-in any function as shown below

if not any([list1, list2, list3]):
    # ...
like image 62
vaultah Avatar answered Sep 27 '22 17:09

vaultah