DEV Community

Poopcoder
Poopcoder

Posted on • Originally published at poopcode.com on

How to check if a list is empty in Python?

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
Enter fullscreen mode Exit fullscreen mode

bool

lst = [1,2,3]

if not bool(list):
    print 'List Empty'
else:
    print 'List not empty'

#List not empty
Enter fullscreen mode Exit fullscreen mode

equal

lst = [1,2,3]

if lst == []:
    print 'List Empty'
else:
    print 'List not empty'

#List not empty
Enter fullscreen mode Exit fullscreen mode

The post How to check if a list is empty in Python? appeared first on Poopcode.

Top comments (1)

Collapse
 
hb profile image
Henry Boisdequin • Edited

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:

print("Hello world!")
Enter fullscreen mode Exit fullscreen mode