In this tutorial, you’ll learn how to use Python to check if a list empty. Python lists are one of the most versatile and widely-used container objects. Because Python lists are iterable, you may often want to first check if a list is empty, before attempting to iterate over a list. Without this, your program may run into issues and crash. Show
By the end of this tutorial, you’ll have learned:
Table of Contents A Quick Recap of Python ListsBefore we dive into learning how to check if a Python list is empty, let’s quickly take a look at what Python lists are and how you can use them. A Python list is a heterogeneous, mutable, and iterable container object in Python. This means that lists can contain items of different data types, have items added, removed or changed, and that you can loop over these items. Let’s take a look at a simple list and how we can iterate over a list:
We can also create an empty list using either of the methods below:
If we were to try and iterate over these lists, nothing would happen. This can lead to some unexpected results. Because of this, checking if a list is empty can be a useful tool to ensure that your program runs as expected. Checking if a Python List is Empty Using ifThe Python The reason I bring any of this up is that PEP 8 actually provides a convention for checking whether or not a list is empty or not. According to PEP 8, the following code is recommended:
Let’s break down how this works: Python assumes that an empty list, just like other empty containers, is represented by 0.Remember, PEP-8 provides conventions to make your code more understandable to other programmers. You don’t need to follow this convention and learning about other ways in which this can be done is important to be able to better understand other people’s code. Checking if a Python List is Empty Using boolOne of the ways you’ll often encounter whether checking if a Python list is empty is using the 1 function. The Python 1 function checks the truthy-ness of an item and returns either 3 or False . In Python, empty objects are regarded as False .Let’s see how we can use the 1 function to see a list is empty:
We can see that this function evaluates whether or not a list is empty correctly. However, it’s also redundant. Recall, from our earlier example, that we can evaluate truthy-ness simply by using the 7 statement.Checking if a Python List is Empty Using EqualityAnother common way to check if a Python list is empty is to compare your list to an empty list using the equality operator 8. This method is quite explicit and, perhaps, beginner-friendly as it makes its intentions very clear.One benefit of this approach is that it makes it clear that you’re checking that the object is a list, rather than some other item. Let’s see how we can use Python to do this:
In the following section, you’ll learn how to use a list’s length to check if its empty. Checking if a Python List is Empty Using Its LengthIn this section, you’ll learn how to check whether a Python list is empty or not by checking its length. Intuitively, a list that is empty has, well, zero items. We can use this information to determine is a list has any items in it.
In the following section, you’ll learn how to check if a list of lists of empty. Checking if a Python List of Lists is EmptyWorking with lists of lists adds another layer of complexity, but it’s nothing that we can intuitively solve with Python! The Python 9 function checks whether any item in a container is 3 and will return False if that’s not the case. Because we now know that a Python list is considered False , we can use this to our advantage.
In the code above, we use the 9 function to check if any item is considered 3. Because this is not the case, the script correctly lets us know that the list of lists is empty.Checking if a NumPy Array is EmptyNumPy arrays are similar to lists in Python (even though this is quite the over-simplification). We can check if an array is empty by using its 5 attribute, which returns the size of that array. Let’s see how we can accomplish this:
Frequently Asked QuestionsWhat is the best way to check if a Python list is empty? The best way, according to PEP-8, is to use the 7 keyword. By writing 7, you can evaluate whether a list is empty or not, since an empty list will return False .Is an empty Python list equal to False? An empty list evaluates to 0, a False value will be returned.ConclusionWorking with Python lists is an essential skill for a Python developer of any level. Being able to check if a list is empty is a simple, but important task to ensure that your program runs in the way that you want it to. In this tutorial, you learned how to check if a list is empty using truthy-ness, length, and equality. You also learned how to check if lists of lists are empty and whether NumPy arrays are empty. How do you check if list is null or empty?isEmpty() method of CollectionUtils can be used to check if a list is empty without worrying about null list. So null check is not required to be placed everywhere before checking the size of the list.
Is an empty list none in Python?Usually, an empty list has a different meaning than None ; None means no value while an empty list means zero values.
How to check empty string in list Python?Method #1 : Using any() + len() The combination of above functions can be used to perform this task. In this, we use any() to check for any element in string and len() is used to get if length of any string is 0 or not.
|