In this tutorial we discuss the various ways of checking if a list is empty in Python.
len
lst = [1,2,3]
if len(lst) == 0:
print 'List Empty'
else:
print 'List not empty'
#List not empty
bool
lst = [1,2,3]
if not bool(list):
print 'List Empty'
else:
print 'List not empty'
#List not empty
equal
lst = [1,2,3]
if lst == []:
print 'List Empty'
else:
print 'List not empty'
#List not empty
The post How to check if a list is empty in Python? appeared first on Poopcode.
Top comments (1)
Nice article. To have syntax highlighting in your articles just add python before the three ticks.
tick, tick, tick python
code
tick, tick, tick python
Output: