# 3 Ways To Check If An Array Is Empty In Python

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.")
```

Output:

``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.")
```

Output:

``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.")```

Output:

``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.

## Wrap Up

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.

If you liked the above tutorial then please follow us on Facebook and Twitter. Let us know the questions and answer you want to cover in this blog.
{"ft_lang_label":"__label__en","ft_lang_prob":0.83236283,"math_prob":0.8765719,"size":3764,"snap":"2022-40-2023-06","text_gpt3_token_len":823,"char_repetition_ratio":0.16542554,"word_repetition_ratio":0.10982659,"special_character_ratio":0.22396387,"punctuation_ratio":0.093589745,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99525625,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-02-07T14:05:35Z\",\"WARC-Record-ID\":\"<urn:uuid:9e9bdd74-97ab-4f5a-9fcc-e93e41d456d2>\",\"Content-Length\":\"73759\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:bec4bcd5-5e84-49fc-94f5-af2a4c8a66a1>\",\"WARC-Concurrent-To\":\"<urn:uuid:295f9355-b2f5-4442-8b3b-071c26b1ceaa>\",\"WARC-IP-Address\":\"52.86.133.10\",\"WARC-Target-URI\":\"https://coduber.com/check-if-an-array-is-empty-in-python/\",\"WARC-Payload-Digest\":\"sha1:WZMUS6FNYRMWUWSZY3VPFPYEP2NCJ2YC\",\"WARC-Block-Digest\":\"sha1:JY6KZ2ZQB254QZXPY4UXYSIRQC24LEJN\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-06/CC-MAIN-2023-06_segments_1674764500619.96_warc_CC-MAIN-20230207134453-20230207164453-00491.warc.gz\"}"}