How do I check if a list is empty?

Asked 2023-09-20 20:19:14 View 104,554

For example, if passed the following:

a = []

How do I check to see if a is empty?

Answers

if not a:
    print("List is empty")

Using the implicit booleanness of the empty list is quite Pythonic.

Answered   2023-09-20 20:19:14

  • Playing devil's advocate. I don't understand why this idiom is considered pythonic. 'Explicit is better then implicit', correct? This check doesn't seem very explicit about what is is checking. - anyone
  • @JamesMcMahon - it's a trade-off between explicitness and type flexibility. generally, "being explicit" means not doing "magical" things. on the other hand, "duck typing" means working with more general interfaces, rather than explicitly checking for types. so something like if a == [] is forcing a particular type (() == [] is False). here, general consensus seems to be that duck typing wins out (in effect, saying that __nonzero__ is the interface for testing emptiness docs.python.org/reference/datamodel.html#object.__nonzero__) - anyone
  • This method doesn't work on numpy arrays.. so I think if len(a) == 0 is preferable both in terms of "duck typing" and implicitness. - anyone
  • The canonical way of knowing if an array in C is empty is by dereferencing the first element and seeing if it is null, assuming an array that is nul-terminated. Otherwise, comparing its length to zero is utterly inefficient if the array is of a significant size. Also, typically, you would not allocate memory for an empty array (pointer remains null), so it makes no sense to attempt to get its length. I am not saying that len(a) == 0 is not a good way of doing it, it just does not scream 'C' to me when I see it. - anyone
  • Coming from a language that claims to be some sort of poetry, this mechanism is pure garbage. Semantically, being empty is very different to not being - anyone

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):

Answered   2023-09-20 20:19:14

  • The second way seems better if you wish to signal that seq is expected to be some sort of list-like object. - anyone
  • @BallpointBen which, Pythonism advocates would say, should be implicit in the way the variable is named, as much as possible - anyone
  • @BallpointBen try using Python's type hinting for signaling what a variable should be. It was introduced in 3.5. - anyone
  • numpy broke this idiom... seq = numpy.array([1,2,3]) followed by if not seq raises an exception "ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()" - anyone
  • Despite all Pythonic advocates, I am with @BallpointBen in that if you mistakenly wrote seq = [0] as seq = 0, len(seq) will help you catch the error. To err is human. So is a programmer. - anyone

I prefer it explicitly:

if len(li) == 0:
    print('the list is empty')

This way it's 100% clear that li is a sequence (list) and we want to test its size. My problem with if not li: ... is that it gives the false impression that li is a boolean variable.

Answered   2023-09-20 20:19:14

  • Checking if the length of a list is equal to zero, rather than just checking if the list is false, is ugly and unpythonic. Anyone familiar with Python will not think li is a bool at all, and wont care. If it's important, you should add a comment, not more code. - anyone
  • This seems like an unnecessarily precise test, which is often slower and is always less readable IMHO. Instead of checking the size of something empty, why not just check if it's empty? - anyone
  • Anyway, the reason this is bad (and that violating idioms in a language with strong idioms like Python is bad in general) is that it signals to the reader that you're specifically checking the length for some reason (e.g., because you want None or 0 to raise an exception rather than passing). So, when you do it for no reason, that's misleading—and it also means that when your code does need to make the distinction, the distinction is invisible because you've "cried wolf" all over the rest of the source. - anyone
  • I think this is just needlessly lengthening the code. Otherwise, why not be even more "explicit" with if bool(len(li) == 0) is True:? - anyone
  • @Jabba it will be O(1) in many cases (those where you work with the built-in data types), but you just can't rely on that. You might be working with a custom data type that doesn't have this property. You might also decide to add this custom data type later, after you already wrote this code. - anyone

This is the first google hit for "python test empty array" and similar queries, and other people are generalizing the question beyond just lists, so here's a caveat for a different type of sequence that a lot of people use.

Other methods don't work for NumPy arrays

You need to be careful with NumPy arrays, because other methods that work fine for lists or other standard containers fail for NumPy arrays. I explain why below, but in short, the preferred method is to use size.

The "pythonic" way doesn't work: Part 1

The "pythonic" way fails with NumPy arrays because NumPy tries to cast the array to an array of bools, and if x tries to evaluate all of those bools at once for some kind of aggregate truth value. But this doesn't make any sense, so you get a ValueError:

>>> x = numpy.array([0,1])
>>> if x: print("x")
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

The "pythonic" way doesn't work: Part 2

But at least the case above tells you that it failed. If you happen to have a NumPy array with exactly one element, the if statement will "work", in the sense that you don't get an error. However, if that one element happens to be 0 (or 0.0, or False, ...), the if statement will incorrectly result in False:

>>> x = numpy.array([0,])
>>> if x: print("x")
... else: print("No x")
No x

But clearly x exists and is not empty! This result is not what you wanted.

Using len can give unexpected results

For example,

len( numpy.zeros((1,0)) )

returns 1, even though the array has zero elements.

The numpythonic way

As explained in the SciPy FAQ, the correct method in all cases where you know you have a NumPy array is to use if x.size:

>>> x = numpy.array([0,1])
>>> if x.size: print("x")
x

>>> x = numpy.array([0,])
>>> if x.size: print("x")
... else: print("No x")
x

>>> x = numpy.zeros((1,0))
>>> if x.size: print("x")
... else: print("No x")
No x

If you're not sure whether it might be a list, a NumPy array, or something else, you could combine this approach with the answer @dubiousjim gives to make sure the right test is used for each type. Not very "pythonic", but it turns out that NumPy intentionally broke pythonicity in at least this sense.

If you need to do more than just check if the input is empty, and you're using other NumPy features like indexing or math operations, it's probably more efficient (and certainly more common) to force the input to be a NumPy array. There are a few nice functions for doing this quickly — most importantly numpy.asarray. This takes your input, does nothing if it's already an array, or wraps your input into an array if it's a list, tuple, etc., and optionally converts it to your chosen dtype. So it's very quick whenever it can be, and it ensures that you just get to assume the input is a NumPy array. We usually even just use the same name, as the conversion to an array won't make it back outside of the current scope:

x = numpy.asarray(x, dtype=numpy.double)

This will make the x.size check work in all cases I see on this page.

Answered   2023-09-20 20:19:14

  • It's worth noting that this isn't a flaw in Python, but rather an intentional break of contract by numpy - numpy is a library with a very specific use case, and it has a different 'natural' definition of what truthiness on an array is to the Python standard for containers. It makes sense to optimise for that case, in the way that pathlib uses / to concatenate paths instead of + - it's non-standard, but makes sense in context. - anyone
  • Agreed. My point is just that it's important to remember that numpy has made the choice to break duck typing for both the very common if x and len(x) idioms -- and sometimes that breakage can be very hard to detect and debug. - anyone
  • I don't know, for me, if a method called len(x) doesn't return the array length because assumptions, it's name is bad designed. - anyone
  • This question has nothing to do with numpy arrays - anyone
  • @ppperry Yes, the original question was not about Numpy arrays, but when working with those and possibly duck typed arguments, this question becomes very relevant. - anyone

Best way to check if a list is empty

For example, if passed the following:

a = []

How do I check to see if a is empty?

Short Answer:

Place the list in a boolean context (for example, with an if or while statement). It will test False if it is empty, and True otherwise. For example:

if not a:                           # do this!
    print('a is an empty list')

PEP 8

PEP 8, the official Python style guide for Python code in Python's standard library, asserts:

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):

We should expect that standard library code should be as performant and correct as possible. But why is that the case, and why do we need this guidance?

Explanation

I frequently see code like this from experienced programmers new to Python:

if len(a) == 0:                     # Don't do this!
    print('a is an empty list')

And users of lazy languages may be tempted to do this:

if a == []:                         # Don't do this!
    print('a is an empty list')

These are correct in their respective other languages. And this is even semantically correct in Python.

But we consider it un-Pythonic because Python supports these semantics directly in the list object's interface via boolean coercion.

From the docs (and note specifically the inclusion of the empty list, []):

By default, an object is considered true unless its class defines either a __bool__() method that returns False or a __len__() method that returns zero, when called with the object. Here are most of the built-in objects considered false:

  • constants defined to be false: None and False.
  • zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  • empty sequences and collections: '', (), [], {}, set(), range(0)

And the datamodel documentation:

object.__bool__(self)

Called to implement truth value testing and the built-in operation bool(); should return False or True. When this method is not defined, __len__() is called, if it is defined, and the object is considered true if its result is nonzero. If a class defines neither __len__() nor __bool__(), all its instances are considered true.

and

object.__len__(self)

Called to implement the built-in function len(). Should return the length of the object, an integer >= 0. Also, an object that doesn’t define a __bool__() method and whose __len__() method returns zero is considered to be false in a Boolean context.

So instead of this:

if len(a) == 0:                     # Don't do this!
    print('a is an empty list')

or this:

if a == []:                     # Don't do this!
    print('a is an empty list')

Do this:

if not a:
    print('a is an empty list')

Doing what's Pythonic usually pays off in performance:

Does it pay off? (Note that less time to perform an equivalent operation is better:)

>>> import timeit
>>> min(timeit.repeat(lambda: len([]) == 0, repeat=100))
0.13775854044661884
>>> min(timeit.repeat(lambda: [] == [], repeat=100))
0.0984637276455409
>>> min(timeit.repeat(lambda: not [], repeat=100))
0.07878462291455435

For scale, here's the cost of calling the function and constructing and returning an empty list, which you might subtract from the costs of the emptiness checks used above:

>>> min(timeit.repeat(lambda: [], repeat=100))
0.07074015751817342

We see that either checking for length with the builtin function len compared to 0 or checking against an empty list is much less performant than using the builtin syntax of the language as documented.

Why?

For the len(a) == 0 check:

First Python has to check the globals to see if len is shadowed.

Then it must call the function, load 0, and do the equality comparison in Python (instead of with C):

>>> import dis
>>> dis.dis(lambda: len([]) == 0)
  1           0 LOAD_GLOBAL              0 (len)
              2 BUILD_LIST               0
              4 CALL_FUNCTION            1
              6 LOAD_CONST               1 (0)
              8 COMPARE_OP               2 (==)
             10 RETURN_VALUE

And for the [] == [] it has to build an unnecessary list and then, again, do the comparison operation in Python's virtual machine (as opposed to C)

>>> dis.dis(lambda: [] == [])
  1           0 BUILD_LIST               0
              2 BUILD_LIST               0
              4 COMPARE_OP               2 (==)
              6 RETURN_VALUE

The "Pythonic" way is a much simpler and faster check since the length of the list is cached in the object instance header:

>>> dis.dis(lambda: not [])
  1           0 BUILD_LIST               0
              2 UNARY_NOT
              4 RETURN_VALUE

Evidence from the C source and documentation

PyVarObject

This is an extension of PyObject that adds the ob_size field. This is only used for objects that have some notion of length. This type does not often appear in the Python/C API. It corresponds to the fields defined by the expansion of the PyObject_VAR_HEAD macro.

From the c source in Include/listobject.h:

typedef struct {
    PyObject_VAR_HEAD
    /* Vector of pointers to list elements.  list[0] is ob_item[0], etc. */
    PyObject **ob_item;

    /* ob_item contains space for 'allocated' elements.  The number
     * currently in use is ob_size.
     * Invariants:
     *     0 <= ob_size <= allocated
     *     len(list) == ob_size

Response to comments:

I would point out that this is also true for the non-empty case though its pretty ugly as with l=[] then %timeit len(l) != 0 90.6 ns ± 8.3 ns, %timeit l != [] 55.6 ns ± 3.09, %timeit not not l 38.5 ns ± 0.372. But there is no way anyone is going to enjoy not not l despite triple the speed. It looks ridiculous. But the speed wins out
I suppose the problem is testing with timeit since just if l: is sufficient but surprisingly %timeit bool(l) yields 101 ns ± 2.64 ns. Interesting there is no way to coerce to bool without this penalty. %timeit l is useless since no conversion would occur.

IPython magic, %timeit, is not entirely useless here:

In [1]: l = []                                                                  

In [2]: %timeit l                                                               
20 ns ± 0.155 ns per loop (mean ± std. dev. of 7 runs, 100000000 loops each)

In [3]: %timeit not l                                                           
24.4 ns ± 1.58 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [4]: %timeit not not l                                                       
30.1 ns ± 2.16 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

We can see there's a bit of linear cost for each additional not here. We want to see the costs, ceteris paribus, that is, all else equal - where all else is minimized as far as possible:

In [5]: %timeit if l: pass                                                      
22.6 ns ± 0.963 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [6]: %timeit if not l: pass                                                  
24.4 ns ± 0.796 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [7]: %timeit if not not l: pass                                              
23.4 ns ± 0.793 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

Now let's look at the case for an unempty list:

In [8]: l = [1]                                                                 

In [9]: %timeit if l: pass                                                      
23.7 ns ± 1.06 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [10]: %timeit if not l: pass                                                 
23.6 ns ± 1.64 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

In [11]: %timeit if not not l: pass                                             
26.3 ns ± 1 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)

What we can see here is that it makes little difference whether you pass in an actual bool to the condition check or the list itself, and if anything, giving the list, as is, is faster.

Python is written in C; it uses its logic at the C level. Anything you write in Python will be slower. And it will likely be orders of magnitude slower unless you're using the mechanisms built into Python directly.

Answered   2023-09-20 20:19:14

  • I would point out that this is also true for the non-empty case though its pretty ugly as with l=[] then %timeit len(l) != 0 90.6 ns ± 8.3 ns, %timeit l != [] 55.6 ns ± 3.09, %timeit not not l 38.5 ns ± 0.372. But there is no way anyone is going to enjoy not not l despite triple the speed. It looks ridiculous. But the speed wins out - anyone
  • I suppose the problem is testing with timeit since just if l: is sufficient but surprisingly %timeit bool(l) yields 101 ns ± 2.64 ns. Interesting there is no way to coerce to bool without this penalty. %timeit l is useless since no conversion would occur. - anyone
  • Best answer so far, thanks! Pointing out the true logic of python with magicmethods and "Python is written in C; it uses its logic at the C level. Anything you write in Python will be slower. And it will likely be orders of magnitude slower" is key. otherwise one falls down into "preferences" and never ends up with a proper conclusion. - anyone
  • Great! This is the best answer! To anyone reading from 2020(it's December so it's almost 2021) and in the future. Doing if l is the "Pythonic" way and BEST way, as this guy explains it very well and also provided some sample code of the time performance calculated for every suggested answer which are if len(a) == 0, if [] == [] and if a So clearly this (if a) is much faster & the one that must be practiced! - anyone
  • You still don't explain why you should use if ls over if len(ls). In 99%, i don't care about this small performance difference and prefer readability. if len(ls) is easier to read, more explicit and it trows an error when something is used that isn't some kind of list, this is why if len(ls) makes more sense. By the way i only get a about 15% performance difference between not len([]) and not []. - anyone

An empty list is itself considered false in true value testing (see python documentation):

a = []
if a:
     print("not empty")

To Daren Thomas's answer:

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.

Answered   2023-09-20 20:19:14

  • Strange how [] == False will evaluate to False though - anyone
  • @information_interchange If you want to explicitly check the truthiness of a value, use bool(). bool([]) == False will evaluate to True as expected. - anyone
  • @information_interchange It really isn't. Consider that if [] == False evaluated to True then, for consistency, so should 0 == False and '' == False, or even '' == []. Note that there's s distinction between truthiness in a boolean context and testing equality. IMHO Clojure does this right: only False and nil are falsy. Python's rule are reasonable. PHP used to have evil rules (haven't used that for a very long time, don't know now). - anyone

Patrick's (accepted) answer is right: if not a: is the right way to do it. Harley Holcombe's answer is right that this is in the PEP 8 style guide. But what none of the answers explain is why it's a good idea to follow the idiom—even if you personally find it's not explicit enough or confusing to Ruby users or whatever.

Python code, and the Python community, has very strong idioms. Following those idioms makes your code easier to read for anyone experienced in Python. And when you violate those idioms, that's a strong signal.

It's true that if not a: doesn't distinguish empty lists from None, or numeric 0, or empty tuples, or empty user-created collection types, or empty user-created not-quite-collection types, or single-element NumPy array acting as scalars with falsey values, etc. And sometimes it's important to be explicit about that. And in that case, you know what you want to be explicit about, so you can test for exactly that. For example, if not a and a is not None: means "anything falsey except None", while if len(a) != 0: means "only empty sequences—and anything besides a sequence is an error here", and so on. Besides testing for exactly what you want to test, this also signals to the reader that this test is important.

But when you don't have anything to be explicit about, anything other than if not a: is misleading the reader. You're signaling something as important when it isn't. (You may also be making the code less flexible, or slower, or whatever, but that's all less important.) And if you habitually mislead the reader like this, then when you do need to make a distinction, it's going to pass unnoticed because you've been "crying wolf" all over your code.

Answered   2023-09-20 20:19:14

  • "And when you violate those idioms, that's a strong signal." It can be a strong signal that you're simply working with code written by someone new to python, which is a lot of people - anyone

Why check at all?

No one seems to have addressed questioning your need to test the list in the first place. Because you provided no additional context, I can imagine that you may not need to do this check in the first place, but are unfamiliar with list processing in Python.

I would argue that the most Pythonic way is to not check at all, but rather to just process the list. That way it will do the right thing whether empty or full.

a = []

for item in a:
    # <Do something with item>

# <The rest of code>

This has the benefit of handling any contents of a, while not requiring a specific check for emptiness. If a is empty, the dependent block will not execute and the interpreter will fall through to the next line.

If you do actually need to check the array for emptiness:

a = []

if not a:
    # <React to empty list>

# <The rest of code>

is sufficient.

Answered   2023-09-20 20:19:14

  • The thing is, check if the list is empty is quite important, at least for me. Have you considered if there's some script inside <rest of code> that might use the result from the for loop? Or directly use some values in a? Indeed, if the script is designed to run with strictly controlled input, the check might be a little unnecessary. But in most cases, the input varies, and have a check is usually better. - anyone
  • Respectfully, no. What I considered was someone who didn’t know enough about Python to know that “if <list>:” was the correct answer, asked how to check for an empty list. Then I notice a LOT of answers that offered differing opinions, but none seemed to address the original need. That is what I tried to do with my answer—have them examine the need before continuing. I believe I suggested as much in my answer, explicitly. - anyone
  • @AmarthGûl - How might one get the results from the for loop to the script inside <rest of code> to be processed? In a list, perhaps? Or maybe a dict? If so, the same logic applies. I'm not understanding how variable input could have any effect within any kind of reasonably designed code, where processing an empty list would be a bad idea. - anyone
  • @DJK - Nope, I think you’re still missing it. Presumably you want to DO something with a list, if you have one. What would you do differently if it were empty? Return early? What if it isn’t empty? process it? The point is, still, that you probably don’t need to check for an empty list, just iterate over it and do whatever you were going to do with the elements. If there are no elements, you fall through. If there are elements, you process them as you need to. The point is NOT to use the example FOR an empty-check but rather to NOT check at all, just process the list. - anyone
  • Good answer, but !a is a syntax error. It should be not a. - anyone

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.

Answered   2023-09-20 20:19:14

I had written:

if isinstance(a, (list, some, other, types, i, accept)) and not a:
    do_stuff

which was voted -1. I'm not sure if that's because readers objected to the strategy or thought the answer wasn't helpful as presented. I'll pretend it was the latter, since---whatever counts as "pythonic"---this is the correct strategy. Unless you've already ruled out, or are prepared to handle cases where a is, for example, False, you need a test more restrictive than just if not a:. You could use something like this:

if isinstance(a, numpy.ndarray) and not a.size:
    do_stuff
elif isinstance(a, collections.Sized) and not a:
    do_stuff

the first test is in response to @Mike's answer, above. The third line could also be replaced with:

elif isinstance(a, (list, tuple)) and not a:

if you only want to accept instances of particular types (and their subtypes), or with:

elif isinstance(a, (list, tuple)) and not len(a):

You can get away without the explicit type check, but only if the surrounding context already assures you that a is a value of the types you're prepared to handle, or if you're sure that types you're not prepared to handle are going to raise errors (e.g., a TypeError if you call len on a value for which it's undefined) that you're prepared to handle. In general, the "pythonic" conventions seem to go this last way. Squeeze it like a duck and let it raise a DuckError if it doesn't know how to quack. You still have to think about what type assumptions you're making, though, and whether the cases you're not prepared to handle properly really are going to error out in the right places. The Numpy arrays are a good example where just blindly relying on len or the boolean typecast may not do precisely what you're expecting.

Answered   2023-09-20 20:19:14

  • It's pretty rare that you're going to have an exhaustive list of 6 types that you want to accept and not be flexible for any other types. When you need that kind of thing, you probably want an ABC. In this case, it would probably be one of the stdlib ABCs, like collections.abc.Sized or collections.abc.Sequence, but it might be one you write yourself and register(list) on. If you actually do have code where it's important to distinguish empty from other falsey, and also to distinguish lists and tuples from any other sequences, then this is correct—but I don't believe you have such code. - anyone
  • The reason people don't like this is because it's entirely unnessesary in most cases. Python is a duck-typed language, and this level of defensive coding actively hinders that. The idea behind Python's type system is that things should work as long as the object passed in functions in the way it needs to. By doing explicit type checks you are forcing the caller to use specific types, going against the very grain of the language. While occasionally such things are necessary (excluding strings from being treated as sequences), such cases are rare and almost always best as blacklists. - anyone
  • If you really want to check that the value is exactly [] and not something falsy of another type, then surely if a == []: is called for, rather than mucking about with isinstance. - anyone
  • There are some automatic coercions for == though. Off the top of my head, I can't identify any for []. [] == () for instance returns False. But for example frozenset()==set() returns True. So it's worth at least giving some thought to whether some undesired type might be coerced to [] (or vice versa) when doing a == []. - anyone
  • @Boris Type hints help static type checkers (like mypy) to check for type correctness but do not perform runtime type checking. - anyone

From documentation on truth value testing:

All values other than what is listed here are considered True

  • None
  • False
  • zero of any numeric type, for example, 0, 0.0, 0j.
  • any empty sequence, for example, '', (), [].
  • any empty mapping, for example, {}.
  • instances of user-defined classes, if the class defines a __bool__() or __len__() method, when that method returns the integer zero or bool value False.

As can be seen, empty list [] is falsy, so doing what would be done to a boolean value sounds most efficient:

if not a:
    print('"a" is empty!')

Answered   2023-09-20 20:19:14

  • @DJ_Stuffy_K assert what in unit testing, an empty list? Just use assert(not myList). If you also want to assert the object is a list, you can use assertIsInstance(). - anyone

Here are a few ways you can check if a list is empty:

a = [] #the list

1) The pretty simple pythonic way:

if not a:
    print("a is empty")

In Python, empty containers such as lists,tuples,sets,dicts,variables etc are seen as False. One could simply treat the list as a predicate (returning a Boolean value). And a True value would indicate that it's non-empty.

2) A much explicit way: using the len() to find the length and check if it equals to 0:

if len(a) == 0:
    print("a is empty")

3) Or comparing it to an anonymous empty list:

if a == []:
    print("a is empty")

4) Another yet silly way to do is using exception and iter():

try:
    next(iter(a))
    # list has elements
except StopIteration:
    print("Error: a is empty")

Answered   2023-09-20 20:19:14

I prefer the following:

if a == []:
   print "The list is empty."

Answered   2023-09-20 20:19:14

  • This is going to be slower, as you instantiate an extra empty list unnecessarily. - anyone
  • this is less readable than if not a: and breaks more easily. Please don't do it. - anyone
  • There is a good point made earlier () == [] is also equal to false. Although I like how this implementation reads the if not a: covers all cases, if you are definitely expecting a list then your example should be sufficient. - anyone
  • "breaks more easily" citation needed? if not a breaks when a is None - you may desire the same behaviour for None and [], but if you explicitly want to check for an empty list, if not a doesn't do that. - anyone
  • @scubbo if you really want to explicitly check whether it's an empty list or not, consider using isinstance(a, list) and not a instead. - anyone

Method 1 (preferred):

if not a:
   print ("Empty")

Method 2:

if len(a) == 0:
   print("Empty")

Method 3:

if a == []:
  print ("Empty")

Answered   2023-09-20 20:19:14

You can even try using bool() like this. Although it is less readable surely it's a concise way to perform this.

    a = [1,2,3];
    print bool(a); # it will return True
    a = [];
    print bool(a); # it will return False

I love this way for the checking list is empty or not.

Very handy and useful.

Answered   2023-09-20 20:19:14

  • For those (like me) who didn't know, bool() converts a Python variable into a boolean so you can store the truthiness or falsiness of a value without having to use an if-statement. I think it's less readable than simply using a conditional like the accepted answer, but I'm sure there are other good use cases for it. - anyone
  • This is usable in an expression and is more terse. - anyone
  • The downside happens when a is None. This is often acceptable, just good to be aware of. - anyone

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"

Answered   2023-09-20 20:19:14

  • Upvoted for also showing how to check if it is not empty! This helps cover the opposite but equally important use case - anyone
  • Note the second one may not work with other common array type objects such as numpy arrays. - anyone
def list_test (L):
    if   L is None  : print('list is None')
    elif not L      : print('list is empty')
    else: print('list has %d elements' % len(L))

list_test(None)
list_test([])
list_test([1,2,3])

It is sometimes good to test for None and for emptiness separately as those are two different states. The code above produces the following output:

list is None 
list is empty 
list has 3 elements

Although it's worth nothing that None is falsy. So if you don't want to separate test for None-ness, you don't have to do that.

def list_test2 (L):
    if not L      : print('list is empty')
    else: print('list has %d elements' % len(L))

list_test2(None)
list_test2([])
list_test2([1,2,3])

produces expected

list is empty
list is empty
list has 3 elements

Answered   2023-09-20 20:19:14

  • IMHO, this is this best answer. It addresses the nuances with respect to None and [] (an empty list) when testing. - anyone

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")

Answered   2023-09-20 20:19:14

  • It is possible this throws an exception, if a is not a list and a has no method __len__ implemented. I would recommend: if isinstance(obj, list): if len(obj) == 0: print '...' - anyone
  • @SvenKrüger nope. Operator and is lazy in Python. Nothing after and is going to be executed if condition before and is False. - anyone

If you want to check if a list is empty:

l = []
if l:
    # do your stuff.

If you want to check whether all the values in list is empty. However it will be True for an empty list:

l = ["", False, 0, '', [], {}, ()]
if all(bool(x) for x in l):
    # do your stuff.

If you want to use both cases together:

def empty_list(lst):
    if len(lst) == 0:
        return False
    else:
        return all(bool(x) for x in l)

Now you can use:

if empty_list(lst):
    # do your stuff.

Answered   2023-09-20 20:19:14

  • all(bool(x) for x in l) is True for an empty list - anyone
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() 

Answered   2023-09-20 20:19:14

We could use a simple if else:

item_list=[]
if len(item_list) == 0:
    print("list is empty")
else:
    print("list is not empty")

Answered   2023-09-20 20:19:14

  • -1 - To avoid confusion, don't use a reserved words for variable names or you may get surprising behavior next time you try to call, for instance "list()"... something like "TypeError: 'list' object is not callable" or some such. - anyone

Being inspired by dubiousjim's solution, I propose to use an additional general check of whether is it something iterable:

import collections
def is_empty(a):
    return not a and isinstance(a, collections.Iterable)

Note: a string is considered to be iterable—add and not isinstance(a,(str,unicode)) if you want the empty string to be excluded

Test:

>>> is_empty('sss')
False
>>> is_empty(555)
False
>>> is_empty(0)
False
>>> is_empty('')
True
>>> is_empty([3])
False
>>> is_empty([])
True
>>> is_empty({})
True
>>> is_empty(())
True

Answered   2023-09-20 20:19:14

  • Overbroad; this is just asking whether a list is empty, not whether something is an empty iterable. - anyone
  • If I wasn't happy with if a:, it would be because I wanted an exception if a wasn't some sort of container. (Being an iterable also allows iterators, which can't usefully be tested for emptiness.) - anyone

Simply use is_empty() or make function like:-

def is_empty(any_structure):
    if any_structure:
        print('Structure is not empty.')
        return True
    else:
        print('Structure is empty.')
        return False  

It can be used for any data_structure like a list,tuples, dictionary and many more. By these, you can call it many times using just is_empty(any_structure).

Answered   2023-09-20 20:19:14

  • The name is_empty suggests that it returns something. But if it did, that something would just be bool(any_structure), which you should use instead (when you need a bool at all). - anyone
  • Why do we want a variation on bool that (also) prints messages to standard output? - anyone
  • @DavisHerring We always have two choice first is to print using function other is using return bool variable. Choice is yours. I write both so you can choose between them. - anyone

Simple way is checking the length is equal zero.

if len(a) == 0:
    print("a is empty")

Answered   2023-09-20 20:19:14

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

Answered   2023-09-20 20:19:14

  • please give more explanation about how it is working without writing "if"? - anyone
  • This is not pythonic nor a complete example. Also, It instantiates an empty list every time it is encountered. Don't do this. - anyone
  • @MrWonderful it does not instantiate an empty list every time. It just verifies if the existing list a is empty or not. - anyone
  • @MrWonderful I don't get what makes it pythonic - anyone
  • @ganeshdeshmukh if you use a==[] it will print true on the python terminal if a is empty. Else it will print False. You can use this inside a if condition also as if(a==[]) - anyone

The truth value of an empty list is False whereas for a non-empty list it is True.

Answered   2023-09-20 20:19:14

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.

Answered   2023-09-20 20:19:14