For example, if passed the following:
a = []
How do I check to see if a is empty?
For example, if passed the following:
a = []
How do I check to see if a is empty?
if not a:
print("List is empty")
Using the implicit booleanness of the empty list is quite Pythonic.
The Pythonic way to do it is from the PEP 8 style guide.
For sequences, (strings, lists, tuples), use the fact that empty sequences are false:
# Correct: if not seq: if seq: # Wrong: if len(seq): if not len(seq):
An empty list is itself considered false in true value testing (see python documentation):
a = []
if a:
print "not empty"
EDIT: Another point against testing the empty list as False: What about polymorphism? You shouldn't depend on a list being a list. It should just quack like a duck - how are you going to get your duckCollection to quack ''False'' when it has no elements?
Your duckCollection should implement __nonzero__ or __len__ so the if a: will work without problems.
len() is an O(1) operation for Python lists, strings, dicts, and sets. Python internally keeps track of the number of elements in these containers.
JavaScript has a similar notion of truthy/falsy.
Method 1 (preferred):
if not a:
print ("Empty")
Method 2:
if len(a) == 0:
print("Empty")
Method 3:
if a == []:
print ("Empty")
To check whether a list is empty or not you can use two following ways. But remember, we should avoid the way of explicitly checking for a type of sequence (it's a less Pythonic way):
def enquiry(list1):
return len(list1) == 0
# ––––––––––––––––––––––––––––––––
list1 = []
if enquiry(list1):
print("The list isn't empty")
else:
print("The list is Empty")
# Result: "The list is Empty".
The second way is a more Pythonic one. This method is an implicit way of checking and much more preferable than the previous one.
def enquiry(list1):
return not list1
# ––––––––––––––––––––––––––––––––
list1 = []
if enquiry(list1):
print("The list is Empty")
else:
print("The list isn't empty")
# Result: "The list is Empty"
Many answers have been given, and a lot of them are pretty good. I just wanted to add that the check
not a
will also pass for None and other types of empty structures. If you truly want to check for an empty list, you can do this:
if isinstance(a, list) and len(a)==0:
print("Received an empty list")
print('not empty' if a else 'empty')
a little more practical:
a.pop() if a else None
and the shortest version:
if a: a.pop()
We could use a simple if else:
item_list=[]
if len(item_list) == 0:
print("list is empty")
else:
print("list is not empty")
From python3 onwards you can use
a == []
to check if the list is empty
EDIT : This works with python2.7 too..
I am not sure why there are so many complicated answers. It's pretty clear and straightforward
What brought me here is a special use-case: I actually wanted a function to tell me if a list is empty or not. I wanted to avoid writing my own function or using a lambda-expression here (because it seemed like it should be simple enough):
foo = itertools.takewhile(is_not_empty, (f(x) for x in itertools.count(1)))
And, of course, there is a very natural way to do it:
foo = itertools.takewhile(bool, (f(x) for x in itertools.count(1)))
Of course, do not use bool in if (i.e., if bool(L):) because it's implied. But, for the cases when "is not empty" is explicitly needed as a function, bool is the best choice.