20

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.

cinemassacres
  • 389
  • 4
  • 16
user2240288
  • 235
  • 2
  • 3
  • 8

5 Answers5

65

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
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • Indeed, empty lists are `False` in a boolean context. No need to explicitly test for `len()`. – Martijn Pieters Apr 15 '13 at 17:26
  • 2
    It should be noted that under the hood, `1` and `2` are really doing the same thing most of the time. – Silas Ray Apr 15 '13 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 Apr 15 '13 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... though `try: l[0]` `except IndexError: #do stuff` is equally silly... – Silas Ray Apr 15 '13 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 Apr 15 '13 at 17:34
8

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"
dawg
  • 98,345
  • 23
  • 131
  • 206
1

Check its length.

l = []
print len(l) == 0
Ali Afshar
  • 40,967
  • 12
  • 95
  • 109
0

this is how you would do that

if len(list3) == 0:
    print("No matches found")  
cinemassacres
  • 389
  • 4
  • 16
Thanakron Tandavas
  • 5,615
  • 5
  • 28
  • 41
0

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().

Kartik
  • 33
  • 1
  • 7