How to check if a list is empty in Python?


Question

The API I'm working with can return empty [] lists.

The following conditional statements aren't working as expected:

if myList is not None: #not working
    pass

if myList is not []: #not working
    pass

What will work?

1
93
12/27/2017 12:20:16 PM

Accepted Answer

if not myList:
  print "Nothing here"
167
11/12/2009 9:25:52 PM

Empty lists evaluate to False in boolean contexts (such as if some_list:).


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon