AppDividend
Latest Code Tutorials

How to Check If a List is Empty in Python

0

Python Lists are just like dynamically sized arrays. A single list may contain Datatypes like Integers, Strings, as well as Objects. Lists are mutable, and that is why they can be modified even after their creation. List in Python are ordered and have a definite count.

Checking if a list is empty using not operator

The if..not is the simplest way to check the empty Python list. First, we define an empty list and then use the if condition with not operator. The not operator checks if it does not find any item in the list then return True. And based on that result, we can say that if the list is empty or not.

listA = []

if not listA:
    print('List is empty')
else:
    print('List is not empty')

Output

List is empty

Python NOT operator returns True if the operand is False.

This is the best Pythonic way to check if the list is empty or not.

Checking empty list using len() Function

The len() function is used to find the number of elements in the list. To check if the list is empty or not using len(), we can pass the empty list to the len() function, and if we get 0, then that means the list is empty.

listA = []

if not len(listA):
    print('List is empty')
else:
    print('List not empty')

Output

List is empty

We can also use the double equal to comparison with 0 to check if the list is empty.

listA = []

if len(listA) == 0:
    print('List is empty')
else:
    print('List not empty')

Output

List is empty

This method is explicit and not more of a Pythonic way, but you get the idea here how you can check.

Compare the empty list with another empty list.

If we compare our current list to [ ], then we will get the idea if the list is empty or not.

listA = []

if listA == []:
    print('List is empty')
else:
    print('List not empty')

Output

List is empty

These approaches are correct in their respective 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, the object is considered True unless its class defines either the __bool__() function that returns False or a __len__() function that returns zero when called with an object. Here are most of the inbuilt objects considered False:

  1. Constants defined to be False: None and False.
  2. Zero of any numeric type: 0, 0.0, 0j, Decimal(0), Fraction(0, 1)
  3. Empty sequences and collections: ”, (), [], {}, set(), range(0).

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.

So the best Pythonic way to check if the list is empty or not is using the following method.

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

Conclusion

Python list is one of the most used data structures in any kind of development and checking if the list is empty or not is the most frequently used operation. So checking it efficiently totally makes sense, and do it in PEP8 way is unavoidable. So in this tutorial, we have seen various ways but suggested the most Pythonic way.

See also

Python list replace

Python list contains

Python add to list

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.