tags:

views:

380

answers:

3

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

What isn't working:

if myList is not None: #not working
if myList is not []: #not working

What will work?

+14  A: 
if not myList:
  print "Nothing here"
Marek Karbarz
Oh jeez, thanks.
Joshua
+3  A: 

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

shylent
A: 

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"
inspectorG4dget
You can do that, but it would violate pep 8, which says:- For sequences, (strings, lists, tuples), use the fact that empty sequences are false. Yes: if not seq: if seq: No: if len(seq) if not len(seq)
Chris Lacasse
Thank you for pointing this out to me, Chris Lacasse. I had no known about pep8, earlier
inspectorG4dget