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.

killertofus
4435 silver badges17 bronze badges
asked Apr 15, 2013 at 17:22
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
answered Apr 15, 2013 at 17:24
5
  • Indeed, empty lists are False in a boolean context. No need to explicitly test for len(). Commented Apr 15, 2013 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. Commented 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. Commented 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... though try: l[0] except IndexError: #do stuff is equally silly... Commented 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. ;-) Commented Apr 15, 2013 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"
answered Apr 15, 2013 at 17:25
1

Check its length.

l = []
print len(l) == 0
answered Apr 15, 2013 at 17:24
0
0

this is how you would do that

if len(list3) == 0:
 print("No matches found") 
killertofus
4435 silver badges17 bronze badges
answered Apr 15, 2013 at 17:25
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().

answered Mar 23, 2020 at 8:45

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.