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 whether a list is empty.
Check if a list is empty by Comparing Directly With an Empty List.
Using the conditional statement, we can directly check if the list is empty.
First, let us look at how an empty list looks like in Python –
list1=list()
print(list1)
Output- []
We can compare the above output 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")
Output- 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 is highly recommended by Python developers. So, let’s look at what the code looks like, and then we will understand what’s happening behind the scenes.
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 the ‘Is’ keyword in Python to check if a list is empty
Program-
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
Explicit comparison
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 we have a numpy array and want to check whether it is empty; we use the above methods. Some of the above methods will give errors, and some will give warning. Don’t trust my words; try using the above methods on a numpy array, and I will do one for you.
import numpy as np
def isempty():
list1=np.array([])
if not list1:
return "empty"
print(isempty())
Output- empty
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))
Output-
EMPTY NOT EMPTY
Must Read
- How to Convert String to Lowercase in
- How to Calculate Square Root
- User Input | Input () Function | Keyboard Input
- Best Book to Learn Python
Conclusion
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 perform some operations on that list. And we have deployed that web application. 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 whether the list is there.
Try to run the programs on your side, and let us know if you have any queries.
Happy Coding!