A list is one of the most commonly used data structures in python, and every Python programmer needs to know about the operations that can be performed on a list. Sometimes as a programmer, before performing any operations on the list in python, we need to check if a list is empty or not. Because if we try performing operations from an empty list, we will get an error.
By empty list, we have a list, but there are no elements in the list. Let us learn some of the most recommended and common ways to check if a list is empty or not.
Check if a list is empty by Comparing Directly With an Empty List.
We can directly check if the list is empty or not using the conditional statement.
First, let us look how an empty list looks like in python-
We can use the above output and compare it with our list to check if it is empty.
Program to remove all the elements of a list
list1=[1,2,3,4,5,6] print("List Before:",list1) # Running while loop till list gets empty while(list1!=): # keep removing elements till there are elements to remove list1.pop() print("List After:",list1)
Output- List Before: [1, 2, 3, 4, 5, 6] List After: 
To check if a list is empty
list2= if list2==: print("Empty") else: print("Not empty")
Using python len() function to check if a list is empty
We can use the len() function to find the length of the list and if the length is equal to 0, it means that the list is empty.
def check_empty(list1): if len(list1)==0: print("List is empty") else: print("List is not empty") list1=[1,2,2,34,55] check_empty(list1) list2= check_empty(list2)
Output- List is not empty List is empty
Note- This is not a pythonic way of doing things and therefore not appreciated/recommended by core python developers. So, try to avoid using this method.
Using ‘not’ keyword in python to check if a list is empty
Using ‘not’ keyword is the most pythonic way and highly recommended by python developers. So, let’s look at how the code looks like, and then we will understand what’s happening behind the scene.
def isempty(list1): if not list1: return("Empty") else: return("Not empty") list2= print(isempty(list2)) list1=[1,2,3,4,5] print(isempty(list1))
Output- Empty Not empty
In the above code, ‘if not list1’ means that if we have nothing in the list, execute the if statement, otherwise, execute the else statement. Python promotes the use of day to day English words, so we should try using this method. Also, computationally it evaluates the results faster.
Using ‘Is’ keyword in python to check if a list is empty
def is_empty(list2): if list2: return("Not empty") else: return('Empty') list2= print(is_empty(list2)) list1=[1,2,3,4,5] print(is_empty(list1))
Output- Empty Not empty
Here, we will compare our list with an empty list.
list1=[1,2,3,45,6,7,8,3,31] list3= def check_empty(list1): empty_list=list() if list1==empty_list: return("Empty") else: return("Not empty") list1=[1,2,3,45,6,7,8,3,31] list3= print(check_empty(list1)) print(check_empty(list3))
Output- Not empty Empty
Checking If Numpy array is Empty
Suppose, if we have a numpy array and we want to check whether it is empty or not, we the above methods. Some of the above methods will give error and some will give warning. Don’t trust my words, try using above methods on numpy array, I will do one for you.
import numpy as np def isempty(): list1=np.array() if not list1: return "empty" print(isempty())
Here, we can clearly see that we are getting a warning that try to avoid such type of methods as they are going to get deprecated in the future.
It is better to use the ‘size’ function of the array to check whether an array is empty or not.
import numpy as np def check_numpy_empty(arr): if arr.size==0: return "EMPTY" else: return "NOT EMPTY" arr=np.array() print(check_numpy_empty(arr)) arr1=np.array([1,2,3,4]) print(check_numpy_empty(arr1))
EMPTY NOT EMPTY
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
While working with the list, it is quite common that we have to check if a list is empty or not. Suppose, we have made a web application using python and we are performing some operations on that list. And we have deployed that web application. Now, a user is using your web application and suddenly your web app crashes. The crash occurred because you were making some operations on an empty list. So, it is always better in python to check if the list is or not.
Try to run the programs on your side and let us know if you have any queries.