What is best way to check if a list is empty in Python?

PythonServer Side ProgrammingProgramming

The best way is to use not operator on list object. If list is empty it returns true, otherwise false.

>>> L1=[]
>>> not L1
True
>>> L1=[1,2]
>>> not L1
False

Another method is to check if length of list is zero which means it is empty

>>> L1=[]
>>> len(L1)
0
>>> L1=[1,2]
>>> len(L1)
2
raja
Published on 30-Jan-2018 00:04:56
Advertisements