How can I find out if a list is empty without using the not command?
Here is what I tried:
if list3[0] == []:
print("No matches found")
else:
print(list3)
I am very much a beginner so excuse me if I do dumb mistakes.
5 Answers 5
In order of preference:
# Good
if not list3:
# Okay
if len(list3) == 0:
# Ugly
if list3 == []:
# Silly
try:
next(iter(list3))
# list has elements
except StopIteration:
# list is empty
If you have both an if and an else you might also re-order the cases:
if list3:
# list has elements
else:
# list is empty
-
Indeed, empty lists are
False
in a boolean context. No need to explicitly test forlen()
.Martijn Pieters– Martijn Pieters2013年04月15日 17:26:19 +00:00Commented Apr 15, 2013 at 17:26 -
2It should be noted that under the hood,
1
and2
are really doing the same thing most of the time.Silas Ray– Silas Ray2013年04月15日 17:26:24 +00:00Commented Apr 15, 2013 at 17:26 -
@sr2222 Sorry. Deleted comment due to syntax error in Python 2.x. Could do
None if list3 else print("No matches found")
in Python 3.x, though.Aya– Aya2013年04月15日 17:31:09 +00:00Commented Apr 15, 2013 at 17:31 -
@Aya You could also do
try: iter(l).next()
except StopIteration: #do stuff
. Of course, 'can' and 'should' are different things... Edit: Doh, John beat me to it... thoughtry: l[0]
except IndexError: #do stuff
is equally silly...Silas Ray– Silas Ray2013年04月15日 17:32:33 +00:00Commented Apr 15, 2013 at 17:32 -
@sr2222 Indeed. Although it'd be interesting to see how many crazy ways people can come up with.
list3 or print("No matches found")
might also do the trick. ;-)Aya– Aya2013年04月15日 17:34:44 +00:00Commented Apr 15, 2013 at 17:34
You find out if a list is empty by testing the 'truth' of it:
>>> bool([])
False
>>> bool([0])
True
While in the second case 0
is False, but the list [0]
is True because it contains something. (If you want to test a list for containing all falsey things, use all or any: any(e for e in li)
is True if any item in li
is truthy.)
This results in this idiom:
if li:
# li has something in it
else:
# optional else -- li does not have something
if not li:
# react to li being empty
# optional else...
According to PEP 8, this is the proper way:
• 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)
You test if a list has a specific index existing by using try
:
>>> try:
... li[3]=6
... except IndexError:
... print 'no bueno'
...
no bueno
So you may want to reverse the order of your code to this:
if list3:
print list3
else:
print "No matches found"
Check its length.
l = []
print len(l) == 0
this is how you would do that
if len(list3) == 0:
print("No matches found")
Python provides an inbuilt any() function to check whether an iterable is empty or not:
>>>list=[]
>>>any(list)
False
The function returns True if the iterable contains a 'True' value, and False otherwise.
However, note that the list [0] also returns False with any().
not
?not
is an operator, not a command