You might be interested in learning how to check in Python whether a list is empty or not. Throughout this article, I’ll explain the most efficient and inefficient methods for determining whether a given list is empty or not.
If you are sending a query to your database and the database responds with a list of elements, this is known as a list of elements scenario.
Furthermore, suppose you want to determine whether or not a given list contains any elements. In order to ensure that the query returned at least one element, it may be necessary to determine whether the list is empty or not.
1. Using If Conditions (Boolean Approach) of Python
If you are familiar with Python If conditions then you must know that using the If condition you can easily tell if the list[1] is empty of not. Alternatively, you can use If not condition as well to verify the list.
Let us see in the below example code the usage of if and if not a condition.
#initializing an empty list givenList = [] #Check if List is Empty using If Condition if givenList: print("List is not Empty") else: print("List is Empty and if conditin is used") #Check List is Empty using If Not Condition if not givenList: print("List is Empty and if not is used")
Output:
List is Empty and if conditin is used
List is Empty and if not is used
Using the above code we got our desired result. But if your intent is to check only if the given list is empty or not it is more efficient to use the if not
condition as that makes more sense and is considered as best practice.
2. Comparing with Empty List to Check List is Empty
The second option, which is not as efficient as the first, is to compare the given list to an empty list, which will return true in this case and allow you to proceed with whatever operation you want to perform.
Let us see in the below example code for comparing the given list with an empty list.
#initializing an empty list givenList = [] #Comparing List with Empty List if givenList == []: print("List is Empty")
Output:
List is Empty
3. Using Len Method of Python
The len() method in Python is used to determine the length or size of a given list. As a result, if you run len() on an empty list, you will get 0 as the result. You can compare whether the len return is 0 or not, and then perform your operation based on the return to see if the given list is empty or not.
It should be noted that this method is not recommended because it is not pythonic to use such code and is poor coding practice.
#initializing an empty list givenList = [] #Using Len to get the Len and COmparing if len(givenList) == 0: print("List is Empty")
Output:
List is Empty
4. Using Try and Except Method (Not Recommended)
This is another method for determining whether or not the list is empty, but it is completely ridiculous and should not be used in production code. I’m including this method solely for educational purposes.
#initializing an empty list givenList = [] #Using Try and Except Method to check if List is Empty try: next(iter(givenList)) except StopIteration: print("Error: List is Empty")
Output:
Error: List is Empty
Wrap Up
I hope you were able to figure out how to check if a list is empty in Python by reading this article. I’ve included a list of approximately four methods that you can use to quickly check the list in Python.
If you have a better method, please let me know in the comments section and I will gladly include it here.
Then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
Further Read: