In this tutorial, you will learn how to check if an array is empty in Python.
Many different scenarios or cases can arise in which you would like to determine whether an array or a list is empty in Python, or if it is not empty. To determine whether an array is empty or not, you can use the if condition, the if not condition, or the len() function, among other techniques.
Let us dive deep with coding examples on how to Check If an array is empty in Python.
1. Using Len() Function
When you use the len() function on an array in Python, it will return the number of elements that the array contains. Now if the array is empty then it will return a value like 0. And if you put a conditional statement to check if it returns 0 then it is sure that the array is empty in Python.
Let us check in the below example code the usage of the len() function on arrays in Python.
#Initializing Empty Array inputList =  #Using Conditional Statement and Len method to #Check if the List is Empty if len(inputList) == 0: print("The input Array is Empty.") else: print("The Input Array is not Empty.")
The input Array is Empty.
It is clear from the code that the len() function returns zero for the input array that I have provided because the list is empty, as shown in the example above. As a result, if the statement is proven to be true,
Because it clearly distinguishes that you are checking the length of the input list and ensuring that it is empty, this method is recommended for use. The code in the following example may appear to indicate that the array is a boolean variable.
2. Using Only If Condition To Check If Array Is Empty
You can also use the if condition itself as a method to accomplish your goal. if condition will treat the array as though it were a boolean variable, and an empty array denotes the None type object, which is equivalent to false in the boolean language.
Let’s take a look at an example code that demonstrates the use of the If condition in Python.
#Initializing Empty Array inputList =  #Using Conditional Statement to #Check if the List is Empty if inputList: print("The input Array is Not Empty.") else: print("The Input Array is Empty.")
The Input Array is Empty.
You can see in the code below that the first if condition doesn’t work because the input array is empty. The else condition is then run. A non-empty input array would meet the first condition in this code. It would then say that the array is not empty.
3. Using Size Method If You Are Using Numpy.Array
If you are using the numpy.array to initialize an array, you will be unable to use either of the two methods listed above. And in this case, you will need to make use of the size() function, which will return the number of elements that are currently present in the array.
Let’s take a look at how the size() function in Python is used on a numpy.array() in the code below.
import numpy as np #Initializing Empty Array inputList = np.array() #Using Conditional Statement to #Check if the List is Empty if inputList.size: print("The input Array is Not Empty.") else: print("The Input Array is Empty.")
The Input Array is Empty.
Numpy arrays have a size method called size that you can use to figure out how big the array is. You can then see if the size of the array is zero, which means the array is empty.
If you were able to understand which method to use when determining whether or not a list or array in Python is empty, I hope this has been helpful. I’ve put together a list of three different approaches you can take to resolve this issue. I hope this helps.