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?
if not myList:
print "Nothing here"
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"
len is an O(1) operation, so that's not really an issue. However, it may be faster (I'm not sure) to simply check if len(myList). Of course, if we head down that way, we may as well do if myList.
!=instead ofis notwould have made this work, though theif myListform is preferred.if myList == []: print("The list is empty!")!=instead ofis notso it'd work