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?

Asked By: y2k

||

Answers:

if not myList:
  print "Nothing here"
Answered By: Marek Karbarz

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

Answered By: shylent

I like Zarembisty’s answer. Although, if you want to be more explicit, you can always do:

if len(my_list) == 0:
    print "my_list is empty"
Answered By: inspectorG4dget
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.