In an effort to start cross-posting, I figured I'd share another brief Python article with everyone. Today, we’re going to learn how to check if a list is empty in Python.
So far, we’ve played a lot with data structures like lists and dictionaries. In terms of lists, we’ve learned how to sum elements of two lists, and we’ve even learned how to convert two lists into a dictionary. However, in all this list manipulation, we’ve never actually discussed how to check if a list is empty.
Detecting an empty list is an important skill to learn as a Python developer. It allows us to handle errors that can occur when attempting to manipulate an empty list. For example, we all know what happens when we try to index an empty list:
Python 3.6.3 (v3.6.3:2c5fed8, Oct 3 2017, 17:26:49) [MSC v.1900 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> my_list = list() >>> my_list Traceback (most recent call last): File "<stdin>", line 1, in <module> IndexError: list index out of range
If we happen to have a function that does some indexing, maybe it’s a good idea to add a protective check for an empty list. After all, handling edge cases is an important skill to learn for any coder.
When it comes to checking if a list is empty in Python, there are a handful of solutions. Let’s dive into a few.
Alright, so the first method for checking if a list is empty is to verify that the length of the list is zero. As someone who learned Java first, I find this solution very intuitive. Take a look:
my_list = list() if len(my_list) == 0: pass # the list is empty
The drawback of this method is that it isn’t very pythonic. After all, the Python community is very strict about its idioms, so it’s probably a good idea to avoid this method if possible.
Another solution, as mentioned by vorsprung, is to compare a list directly to an empty list:
my_list = list() if my_list == : pass # the list is empty
This solution works just as well as the previous version, but it's also not considered pythonic. The reason being that Python has two different types of list-like collections: tuples and lists. Tuples are immutable lists, and comparing a empty tuple to an empty list,
() == , would return false.
Regardless, both solutions are valid. In the next section, we'll cover the community accepted list emptiness test.
Dynamic typing is a fun feature of Python because it allows a variable to assume many forms. While someone with a static typing background might find this frustrating, others will find that dynamic typing in Python has its perks. Among those perks is the fact that empty sequences evaluate to false.
So, what does that mean in terms of code? Well, it means we can treat a list like a boolean. For example:
my_list = list() if not my_list: pass # the list is empty
Now, this may not seem very intuitive, but it is the accepted PEP 8 standard. In other words, this is the method we should use in production code.
Today’s topic is how to check if a list is empty in Python. As always, in this section, we do a little recap of the solutions we’ve shared above:
my_list = list() # Check if a list is empty by its length if len(my_list) == 0: pass # the list is empty # Check if a list is empty by direct comparison if my_list == : pass # the list is empty # Check if a list is empty by its type flexibility **preferred method** if not my_list: pass # the list is empty
And with that, we’re finished. Once again, thanks for stopping by. Hopefully, this was helpful!