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.

4
  • 3
    What's wrong with not? Commented Apr 15, 2013 at 17:24
  • 1
    Its for a school task, and I'm not supposed to know not right now. I only know about it because I searched for the answer before. Commented Apr 15, 2013 at 17:29
  • possible duplicate of Best way to check if a list is empty Commented Jan 2, 2014 at 6:42
  • Top tip: not is an operator, not a command Commented Aug 21, 2017 at 6:04

5 Answers 5

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
Sign up to request clarification or add additional context in comments.

5 Comments

Indeed, empty lists are False in a boolean context. No need to explicitly test for len().
It should be noted that under the hood, 1 and 2 are really doing the same thing most of the time.
@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 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...
@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. ;-)
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"

Comments

1

Check its length.

l = []
print len(l) == 0

Comments

0

this is how you would do that

if len(list3) == 0:
    print("No matches found")  

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.