Lists are the most commonly used data structures in python. It is used to store multiple items in a single object. Lists are,
- Mutable Elements can be added or removed
- Iterable You can iterate the list to access the elements in the list
Before accessing the list items, you may need to check if the list is empty or not as a programming best practice.
You can check if the list is empty using the len[] function in python.
In this tutorial, youll learn how to check if the list is empty or not in python.
If Youre in Hurry
You can use the below code snippet to check if the list is empty in Python.
This is the recommended method in PEP-8 and it is the best way to check if the list is empty.
Snippet
a =[] if not a: print["List is empty"] else: print["List is not empty"]Output
List is EmptySnippet 2
a =[] if a: print["List is not empty"] else: print["List is Empty"]Output
List is EmptyIf You Want to Understand Details, Read on
In this tutorial, youll learn the different methods available to check if the list is empty or not in python.
Table of Contents
- Using PEP-8 Recommended Method
- Using The bool[] Function
- Using len[] Function
- Using len[] With Comparison Operator
- Comparison With Empty List
- Check if Nested List is Empty
- In Case of Numpy Array
- Why You need to check If List is Empty
- Conclusion
- You May Also Like
Using PEP-8 Recommended Method
You can check if the list is empty or not by using the list name in the If statement.
When you are using the list in the IF statement, the length of the list is returned. If the length is 0, its implicitly converted to False. If length is greater than 0, then its converted to True. This method is also called Truth Value Testing.
In the below example, youre using the If not to check if the list is empty.
So you can implement the logic that needs to be executed when the list is empty in the If part.
Snippet
a =[] if not a: print["List is empty"] else: print["List is not Empty"]Output
List is emptyIn the below example, youre using only the If to check if the list is empty.
So you can implement the logic that needs to be executed when the list is not empty in the If part.
Snippet
a =[] if a: print["List is not empty"] else: print["List is Empty"]Output
List is EmptyThis is the fastest way to check if the list is empty in python.
Using The bool[] Function
You can check if the list is empty in python using the bool[] function.
bool[] function returns boolean value of the specified object. The object will always return True, unless the object is empty, like [], [], {}. You can use the bool function for any of the list-like objects.
Use the below code snippet to check if the list is empty or not using the bool[] function.
Snippet
a =[] if bool[a]: print["list is not empty"] else: print["list is empty"]Output
list is emptyIf you want to perform some operation if the list is empty, then you can use if not bool and implement the code that needs to be executed when the list is empty.
Snippet
a =[] if not bool[a]: print["list is empty"] else: print["list is not empty"]Output
list is emptyThis is how you can use the bool[] function to check if the list is empty or not in Python.
Using len[] Function
In this section, youll learn how to use the len[] function to check if the list is empty or not in python.
len[] function returns the number of items in the list.
When the list is empty, the len[] function returns 0, and the 0 is implicitly converted to False when used in the If statement. Values other than 0 are converted to True implicitly.
Use the below snippet to check if the list is empty or not in python using the len[] function and If not.
Snippet
a = [] #Length returns 0 if list is empty. 0 is implicitly converted to #false when used in the IF statement if not len[a]: print['The list is empty'] else: print['list is not empty']Output
The list is emptyYou can use the len[] function alone to check if the list is not empty before performing any operation.
Snippet
a = [] #Length returns 0 if the list is empty. 0 is implicitly converted to false #when used in IF statement if len[a]: print['The list is not empty'] else: print['list is empty']Output
The list is emptyThis is how you check if the list is empty or not in python using the len[] function.
Using len[] With Comparison Operator
You can use the len[] function with the comparison operator and compare the result with 0 to check if the list is empty.
If the list is empty, then the If statement will be executed.
Snippet
a = [] if len[a] == 0: print['List is empty'] else: print['List not empty']Output
List is emptyThis is how you can use the len[] function with the comparison operator to check if the list is empty or not in python.
Comparison With Empty List
You can also compare the list object with the empty list to check if the list is empty.
An empty list is denoted using the []. When a list object is compared with [] using == operator, then it returns True if the list object is empty. Else it returns False.
Use the below snippet to check if the list is empty by comparing it with the empty list.
Snippet
Output
List is emptyThis is how you can use compare the list with the empty list to check if its empty or not.
Check if Nested List is Empty
In this section, youll learn how to check if the nested list is empty. Nested lists are lists that contain multiple list objects as elements.
You can check if the nested list is empty by using the not and any[] function of python.
any function will check if any of the lists inside the nested lists contain any value in them. If not, then itll return True when means the nested list is empty.
Use the below statement to check if the nested list is empty.
Snippet
a = [[],[]] if not any[a]: print['List is empty'] else: print['List is not empty']Output
List is emptyThis is how you can check if the nested list is empty or not by using the any[] function.
In Case of Numpy Array
When using the list values in machine learning programs, you may need to convert the list into the NumPy arrays. In such scenarios, if you want to check if the NumPy array is empty or not, then you can use the .size attribute of the NumPy array to check if the array created out of a list is empty or not.
Use the below snippet to check if the NumPy array is empty or not.
Snippet
import numpy lis = [0, ] if numpy.array[lis].size: print["List is Not Empty"] else: print["List is Empty"]Output
List is Not EmptyWhy You need to check If List is Empty
If youre just checking if the list is empty or not Just to ensure its not empty before performing any operation, then you can pretty well use the list in the for loop or any other iterators directly. Itll be executed only if the list has any items. Otherwise, it will not be executed.
Snippet
a = [] for element in a: print[element]Conclusion
To summarize, youve learned how to check if a list is empty or not using the pep8 recommended method. It is the fastest way and the best way to check if the list is empty. Youve also learned other methods available to check if the list is empty such as bool[] function, len[] function, compared with the empty list, and so on.
If you have any questions, comment below.
You May Also Like
- Python List To String Definitive Guide