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


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

Updated on: 16-Jun-2020

235 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements