How to Check if a List Is Empty Python
You may have used many data types and variables in different programming languages. Therefore, you must have heard about the Python List data structure. The list data structure is quite known and often used data structure to iterate the data within the code. Due to the diversity of the Python language, we can apply a lot of built-in methods to list data structure. However, have you ever tried some ways to see if the list data structure is empty or full? If not, and you are looking for methods to do so, then you are at the right place. Within this article guide, we will discuss some examples and some simple, easy-to-do methods to check if the list data structure is empty or not. So, let's get started. Make sure you have Spyder3 perfectly configured on your Windows 10.
Example 1: Using PEP 8 Recommended Method
Within this method, we will be using the truth value testing to see if the list is empty or not. The truth value testing method returns true or false in return. So, we have formed a new project entitled "temp.py" in Spyder3. A string type of list "list1" has been initialized containing up to 5 values. Another list, "list2", has been initialized empty.
The truth value testing method contains an "if-else" statement to get the "truth" value. Here, we have used it to check if the "list2" is filled or empty. If the list is not empty, it will execute the print statement in the "if" section saying that list is not empty. Otherwise, it will display that the specified list is empty. The sample code is attached below:
list1 = [ "hello" , "readers" , "Welcome" , "to" , "Linuxhint" ]
list2 = [ ]
if list2:
print ( "The above list is non-empty" )
else:
print ( "The above list is empty" )
After running this code with the "Run" button on the Spyder3 taskbar, we have the following result. It shows that the specified list is empty. Thus, it returns as False:
Example 2
Let's take another look at the truth value testing method with a little change. Within this example, we will be using the "not" built-in "if" clause within the Python code. So, open the project "temp.py" and update the code. Add the word "not" in the "if" statement first. Also, exchange the positions of both print statements with each other. This is to get the "True" result in return. As the list2 is empty, it will execute the "print" statement of the "if" clause. Thus, it will return as True. The sample code is attached below:
list1 = [ "hello" , "readers" , "Welcome" , "to" , "Linuxhint" ]
list2 = [ ]
if not list2:
print ( "The above list is empty" )
else:
print ( "The above list is non empty" )
Execute the updated project Python code with the "Run" button from the top taskbar of Spyder3. As the list2 is unfilled, it returns True. Therefore, the first print statement is printed out, showing that the list2 is empty.
Example 3: bool() Function
The keyword "bool" states the Boolean values, i.e., true or false. So, within this example, we will be applying the built-in bool() method on the list data structure to see if the list is empty or not. So, the same project has been updated. We have applied the "bool()" built-in method on "list2" which is empty. Upon checking, the "bool()" method will return true or false and execute the related print statement as a result. The sample code is attached below:
list1 = [ "hello" , "readers" , "Welcome" , "to" , "Linuxhint" ]
list2 = [ ]
if bool (list2):
print ( "The above list is empty" )
else:
print ( "The above list is non empty" )
Let's run the updated code once again by using the Run button from the taskbar of Spyder3. As the list was empty, the bool() method got false in return. Hence, the second print statement has been executed, stating that the list is not empty.
Conclusion
Today, we have discussed two easy and most-known methods to check if the list is empty in Python. To sum up, we are confident that all the methods implemented above are easy to do and can be implemented with any Python tool.
About the author
Hello, I am a freelance writer and usually write for Linux and other technology related content
How to Check if a List Is Empty Python
Source: https://linuxhint.com/how-to-check-if-a-list-is-empty-in-python/