There are very many ways and options to check if a list is empty in Python Programming language. Before getting into the syntax of the solutions, first let us lay out the different factors involved in deciding which method we want to use.

The expression can fall into one of two categories/methods:

  • The explicit comparison
  • The implicit evaluation

What does that these two comparisons mean?

The Explicit Comparison

Whether we use list notation [] or an empty function list(), the strategy is to see if our list in question is exactly the same as an empty list.

# both the list notation and the function list create an empty list
a = []
b = list()

We can use the len() function to return the actual count of items in the list.

print (a ==b) 
a = []
if len(a) == 0:
   print("The list is empty")

The Implicit Evaluation

Conversely, implicit evaluation leverages on the way in which an empty list will be evaluate as a False boolean value. While the populated list will get to be evaluated as a True boolean value.

a = []
b = [1]

if a:
   print("Evaluated True")
else:
   print("Evaluated False")

if b:
   print("Evaluated True")
else:
   print("Evaluated False")

"""
Evaluated False
Evaluated True
"""

What is the Difference?

One can easily use the explicit comparisons. However, if you are trying to follow the practice of duck typing, then you work with the implicit method.

Duck Typing is?

Duck typing functions by prioritizes convenience over safety. This allows for more flexible code that can adapt to a wider range of uses. Furthermore, it is also less strict than the traditional conventions.

From the phrase: “if it walks like a duck and it quacks like a duck then it is most likely must be a duck.”

Functionally, it is recognized as an acknowledgment that less stress will be put on the object’s actual data type. Rather, emphasis will be placed on its attribute’s behaviors, such as being iterable. Ultimately, the goal is to become type agnostic.

So What Technique Should One Use?

By working with both ways, I have become comfortable and am preferential towards the implicit evaluation; understanding that an empty list will evaluate to False.

a = []
print(bool(a)) # False

The abovr function allows me to consolidate longer expression checks, like the one below:

# BEFORE
if isinstance(a,list) and len(a) > 0:
   print("Processing list...")

# AFTER
if a:
   print("Processing list...")

In Conclusion

  • The choice will come down to implications of the empty list.
  • If you are checking if a list is empty because you want to iterate over it or not. This is because Implicit evaluation is the path of least resistance.
  • If you are checking for an empty list because your code is planning on using list methods next or not. Using the explicit comparison to further validate the data type could work well in this scenario.

So, which method do you prefer to use? What are your determining factors when deciding on which technique to use to check for an empty list? Let me know.

If you have any question or comment, do not hesitate to ask us.

Quote: The moon looks upon many night flowers; the night flowers see but one moon. – Jean Ingelow

Source courtesy of Jonathan Hsu (Better Programming)

Advertisement