Show A list is a frequently used data structure in Python. Therefore, users often need to determine if a list is empty or not. There are several ways to do this. Some of the ways may appear to be more explicit while others take relatively less time to execute and hence, are endorsed by the developers of Python.
First, let’s take a look at the most “Pythonic” and recommended way to do determine if a list is empty. An empty data structure is always evaluated to false in Python, and the following method relies on this boolean interpretation of a data structure in the language. As mentioned earlier, an empty list evaluates to false; when the not operator is applied on false, the result is true and hence, the code inside the if condition is executed. Pro
# RECOMMENDED empty_list = [] if not empty_list: print('The list is empty!') else: print('The list contains something.')
Now, let’s look at a method that seems obvious: the built-in length function. len returns 0 if the list is empty. Con
empty_list = [] if len(empty_list) == 0: print('The list is empty!') else: print('The list contains something.')
Lastly, consider another approach that seems pretty straightforward. Here, compare_with is another empty list and we compare our own list with it. Con
empty_list = [] compare_with = [] if empty_list == compare_with: print('The list is empty!') else: print('The list contains something.') Lists are one of the four most commonly used data structures provided by Python. Its functionality, extensibility, and ease of use make it useful for implementing various types of functionalities. Python lists have a few interesting characteristics:
The main attribute that will be focusing on is Iterability. An important part when dealing with an iterable object, in this case a list, is checking if there's anything to iterate through. If not handled properly, this can lead to a lot of unwanted errors. Python provides various ways to check if our list is empty or not, some implicit and some explicit, and in this article, we'll go over how to check if a Python list is empty. Using len() FunctionOne of the techniques is to use the len() function to check if our list is empty or not: py_list = [] """ Here len() returns 0, which is implicitly converted to false """ if len(py_list): print('The list is not empty') else: print('T list is empty')Output List is emptyWhen len(py_list) executes it produces zero, which is then implicitly cast to the boolean value of False. Thus in case of an empty list the program will be redirected to the else block. Although this method looks simple, it's not that intuitive for beginners. Using len() With Comparison OperatorThis technique is similar to the one above but it is more explicit and easy to understand. That's why those who are new to python or coding itself usually consider it more intuitive: if len(py_list) == 0: print('List is empty') else: print('List not empty')In the code above, len(py_list) == 0 will be true if the list is empty and will will be redirected to the else block. This also allows you to set other values as well, rather than relying on 0 being converted as False. All other positive values are converted to True. Comparison With Empty ListThis method is also very simple and works well for beginners as it involves comparing with an empty list: if py_list == []: print('List is empty') else: print('List is not empty')Here again, we are using the comparison operation to compare one list with another - am empty one, and if both are empty the if block will execute. Pep-8 Recommended Styleif py_list: print('List is not empty') if not py_list: print('List empty')For this, let's take a look at Truth Value Testing. The official docs state that:
As an empty list is in fact just a empty collection, it will be converted to a boolean value of False. Therefore, if py_list is empty, it will converted to False. The second statement is pretty similar, except not will invert the a false condition to a true one. This approach is very similar to the if(len(list)) approach. This is the preferred approach as it's the cleanest and shortest solution there is. Using bool() FunctionWe can also use the bool() function to verify a list is empty: Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! if bool(py_list): print('List is not empty') else: print('List is empty')This is basically a manually implemented truth value test. So if the list is not empty the function will return True and if block will be executed. This approach is less common as we can achieve the desired results even without using bool(), but it's not a bad thing to know how Python works under the hood. ConclusionThis article was all about ways to check if our python list is empty or not. We started by exploring different techniques and finally looking into some parameters that we can use to make our judgment regarding which technique may work for us. I can't say that this is the end as new Python updates may give rise to new and more improved coding styles. So it's better to keep exploring and keep learning. |