Believe it or not—how you answer this question in your day-to-day code reveals your true Python skill level to every master coder who reads your code.
Beginner coders check if a list
a is empty using crude statements like
a==. While those solve the problem—they check if a list is empty—they are not what a master coder would do. Instead, the most Pythonic way to check if a list (or any other iterable for that matter) is empty is the expression
You may call it implicit Booleanness (or, more formal, type flexibility): every object in Python can be implicityl converted into a truth value.
Here’s an example in our interactive Python shell—try it yourself!
Exercise: What’s the output of the code if you add one element to the list
Truth Value Testing and Type Flexibility
Python implicitly associates any object with a Boolean value. Here are some examples:
- The integers 1, 2, and 3 are associated to the Boolean
- The integer 0 is associated to the Boolean
- The strings
'0'are associated to the Boolean
- The empty string
''is associated to the Boolean
Roughly speaking, each time a Boolean value is expected, you can throw in a Python object instead. The Python object will then be converted to a Boolean value. This Boolean value will be used to decide whether to enter, say, a
while loop or an
if statement. This is called “type flexibility” and it’s one of Python’s core design choices.
Per default, all objects are considered
True if they are semantically non-empty. Empty objects are usually associated to the Boolean
False. More specifically, only if one of the two cases is met, will the result of an object be
False: (i) the
__len__() function returns 0, or (ii) the
__bool__() function returns
False. You can redefine those two methods for each object.
From the Python documentation, here are some common objects that are associated to the Boolean
- Defined constants:
- Zero of numerical types:
- Empty iterables:
Here are some examples:
if : print('1') if (): print('2') if [()]: print('3') # 3 if 0: print('4') if 0.00: print('5') if 0.001: print('6') # 6 if set(): print('7') if [set()]: print('8') # 8
Again, even if the iterable contains only a single element (that may evaluate to
False like integer
0), the implicit Boolean conversion will return
True because an empty element is an element nonetheless.
PEP8 Recommendation: How to Check if a List is Empty
As some readers argued with me about how to correctly check for an empty list in Python, here‘s the explicit excerpt from the PEP8 standard (Python’s set of rules about how to write readable code):
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):
To see which of the three methods is fastest, I repeated each method 100 times using the
timeit library on my notebook with Intel Core i7 (TM) CPU of 8th Generation, 8GB RAM—yes, I know—and NVIDIA Graphic Card (not that it mattered).
Here’s the code:
import timeit import numpy as np setup = 'a = ' method1 = 'if len(a) == 0: pass' method2 = 'if a == : pass' method3 = 'if not a: pass' t1 = timeit.repeat(stmt=method1, setup=setup, repeat=100) t2 = timeit.repeat(stmt=method2, setup=setup, repeat=100) t3 = timeit.repeat(stmt=method3, setup=setup, repeat=100) print('Method 1: len(a) == 0') print('avg: ' + str(np.average(t1))) print('var: ' + str(np.var(t1))) print() print('Method 2: a == ') print('avg: ' + str(np.average(t2))) print('var: ' + str(np.var(t2))) print() print('Method 3: not a') print('avg: ' + str(np.average(t3))) print('var: ' + str(np.var(t3))) print()
The third method is the most Pythonic one with type flexibility. We measure the elapsed time of 100 executions of each method. In particular, we’re interested in the average time and the variance of the elapsed time. Both should be minimal.
Our thesis is that the third, most Pythonic method is also the fastest because there’s no need to create a new empty list (like in method 2) or performing nested function calls like in method 1. Method 3 consists only of a single function call: converting the list into a Boolean value with the
Here’s the result in terms of elapsed average runtime and variance of the runtimes:
Method 1: len(a) == 0 avg: 0.06273576400000003 var: 0.00022597495215430347 Method 2: a ==  avg: 0.034635367999999944 var: 8.290137682917488e-05 Method 3: not a avg: 0.017685209000000004 var: 6.900910317342067e-05
You can see that the third method is not only 50% faster than method 2 and 75% faster than method 3, it also has very little variance. It’s clearly the best method in terms of runtime performance. Being also the shortest method, you can now see why the method is considered to be most “Pythonic”.
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.