# Check if NumPy Array is Empty in Python + Examples

In this Python tutorial, we will discuss how to check if NumPy array is empty in Python and also we will cover the below points:

1. Check if Numpy Array is Empty
2. Check if Numpy Array is numeric in Python
3. Check if Numpy Array is all zeroes
4. Check if Numpy Array is a subset of another array
5. Check if Numpy Array is all same value in Python
6. Check if Numpy Array is a view
7. Check if Numpy Array is float in Python
8. Check if Numpy Array is in the list
9. Check if Numpy Array is sorted
10. Check if the Numpy array has negative values
11. Check if Numpy array is Concatenate()
12. Check if a value exists in a Numpy array
13. Check if the Numpy array is Nan
14. Check if Numpy array has Duplicates
15. Check if the NumPy array is 1d or 2d

## Check if Numpy Array is Empty

Numpy is a popular library in Python used to deal with arrays. Arrays can be single, double, or multiple dimensional. Numpy is widely used for performing scientific calculations, matrix operations, and is the major component of Machine Learning and Data Science.

• In this section, we will learn how to check if the array is empty or not using Numpy.
• Numpy offers multiple methods using which we can identify whether the array is empty or not. Here is the list of methods:
• numpy.any()
• array.shape()

numpy.any()

• numpy.any() is a method that checks if the given array is empty or not.
• numpy.any() accepts an array as an argument and then returns True for empty and False for a non-empty array.

Syntax:

Here is the syntax of numpy.any() in in NumPy using Python.

``````any(
a,
axis=None,
out=None,
keepdims=<no value>,
*,
where=<no value>
)``````

code:

In this code, we have created an array in Python.

Let’s take an example to check whether an array is empty or not by using numpy.any method.

``````import numpy as np

arr1 = np.array([])
ran = not np.any(arr1)

if ran:
print('Array is empty')
else:
print('Array is not empty')

``````

Arr.shape(): This is an attribute of the NumPy exhibit that profits a tuple giving the state of the array. We can utilize this to check if the NumPy is an array or not.

Now let’s take an example and use Arr. shape attribute in the NumPy array and then check an array is empty or not.

``````import numpy as np

arr2 = np.array([])
if arr2.shape == 0:
print("Array is Empty")``````

Here is the screenshot of following given code

The above code we can use to check if NumPy arrary is empty or not.

## Python Check if Numpy Array is Numeric

• In this section, we will learn how to check if the Numpy Array is Numeric in Python.
• The numeric() function checks whether the character of the string is numeric characters or not.

Let’s take an example to check the numeric character using isnumeric() method:

Here is the screenshot of the following given code:

``````import numpy as np

y = np.char.isnumeric('1')
def isnumeric(y):
try:
float(y)
return True #if it is numeric value it returns true
except ValueError:
return False #if it is not numeric value it returns false
print(y)``````

This is how to check if Numpy array is numeric.

## Check if Numpy Array is all zeroes in Python

• In this section, we will learn how to check if the Numpy array is all zeroes.
• Numpy Np.all() function to show an element of a given array is all zeroes or not.

In this function pass array as parameter. If one of the elements is zero then it returns false otherwise it returns true.

``````import numpy as np
dt = np.array([0, 0, 0, 0, 0, 0])
all_zero = not np.any(dt)
if all_zero:
print('Array contains only 0')
else:
print('Array has non-zero items ')``````

You can refer to the below screenshot to see the output for Check if Numpy Array is all zeroes

This is how to check if Numpy array is all zeroes.

## Check if Numpy Array is a Subset of another array

Subset of an array

• Suppose if we are taking two arrays the array A is called the subset of array B when all the elements of array A are contained in the array B.

Let’s take an example. A is an array consists of elements [2,4,6,7,8]. The array B consists of elements [2,4,6]. Now we can say that B is a subset of array A. Because it contains all the elements of array A.

``````import Numpy as Np
p = int(input("Enter the length of an array:-"))
a=[]
for i in range(p):
a.append(int(input("ELEMENTS:-")))
a=Np.array(a)

m=int(input("Enter the length of the sub array:-"))
b=[]
for j in range(m):
b.append(int(input("ELEMENTS:-")))
b=Np.array(b)
if m>p:
print("NOT SUB ARRAY ")
else:
count=0
for i in b:
for j in a:
for i ==j:
count+=1
if count ==m:
print("It is subset array")
else:
print("NOT sub array")
``````

You can refer to the below screenshot to see the output for Check if Numpy Array is subset of another array

The above python code, we can use to check if NumPy Array is a Subset of another array.

## Check if Numpy Array is all same value in Python

• In this section, we will learn how to check if the Numpy array is all same values.
• Check if all elements are same using Np.all() function. As our Numpy array contains only integers.

Let’s take an example to show whether Numpy array is all same value or not

``````import numpy as Np
arr1 = Np.array([3,3,3,3,3])
res = Np.all(arr1 == arr1)
if res:
print('All values in Array are same')
else:
print('All values in Array are not same')
``````

You can refer to the below screenshot to see the output for Check if Numpy Array is all same value

This is how to check if Numpy Array is all same value in Python.

## Check if Numpy Array is a view

• In this section, we will learn how to check if the Numpy array is a view.
• In a Numpy array, the view does not own the data and any changes made to the view will affect the original array, and any changes made to the original array will affect the view

Let’s take an example to check if Numpy Array is a view in Python,

``````import numpy as Np

arr = Np.array([2,4,6,8])

y = arr.view()
arr = 32

print(arr)
print(y)
``````

The above Python code, we can use to check if Numpy Array is a view in Python.

## Check if NumPy Array is Float in Python

Float is used to represent real numbers for e.g 1.1,2.3. The data type can be specified using a string like ‘F for float.

Let us take an example to show the data type of an array

``````import numpy as np

arr = np.array([1.1, 2.1, 3.1])
arr2 = np.array([2,3,4,5])

newarr = arr.astype('i')
if arr:
print('Array is Float')
else:
print('Array is not Float ')
print(newarr)
print(newarr.dtype)``````

You can refer to the below screenshot to see the output for Check if NumPy Array is Float

This is how to check if NumPy array is float.

## Check if Numpy Array is in list

• In this section, we will learn how to check if Numpy Array is on the list.
• Create a Numpy array in a list we have to pass the list object to the Numpy array

Let’s take an example to show how to create a list in an array .

``````import numpy as np
arr =([2,3,4,5])
def inList(array, list):
for element in list:
if np.array_equal(element, array):
return True
return False``````

You can refer to the below screenshot to see the output for Check if Numpy Array is in list

This is how to check if NumPy array is in list in Python.

## Python Check if Numpy Array is sorted

SORTING means putting elements in an ordered sequence. The Numpy array has a function called .sort() which will arrange the elements in a sequence order

• In this section, we will learn how to check if Numpy Array is sorted
• Create a Numpy array in a list we have a function .sort() which will arrange the elements in a sequence order

Let’s take an example to show how array is sorted

``````import numpy as np

arr1= np.array([6, 9, 2, 0]) # array is not sorted
arr2 = np.arange(4,5,6) # array is sorted

def sorted(x):
return (np.arange(len(x)) == np.argsort(x)).all()
print(sorted(arr1))
print(sorted(arr2))``````

You can refer to the below screenshot to see the output for Check if Numpy Array is sorted

This is how to check if NumPy arrary is sorted in Python.

## Check if the Numpy array has negative values

• In this section, we will learn how to check if the Numpy array has negative values
• Create a Numpy array in a list and then use the function Np.negative().

Numpy array has a function called Np.negative(). When we need to process the negative values of array elements

Let’s take an example to show how a positive elements convert into negative elements

``````import numpy as np

arr1= np.array([6, 9, 2, 0])
b = np.any(arr1<0) #check for negative
print(b)``````

You can refer to the below screenshot to see the output for Check if Numpy array has negative values

The above code, we can use to check if the Numpy array has negative values in Python.

## Check if Numpy array is Concatenate() in Python

• In this section, we will learn how to check if the Numpy array is Concatenate.
• Create a Numpy array in a list and use the function Np.concatenate(). It will combine two given arrays

Numpy Concatenate function can be used to combine two given arrays either row-wise or column-wise. First, we create a tuple with all arrays and then use the function Np.concatenate().

Let’s take an example to show how the given arrays are going to append

``````import Numpy as Np

arr = Np.array([2,6,7,4,8,9])
arr 2 = Np.array([4,6,7,2,3,5])
a = Np.concatenate((arr,arr 2))
print(a)``````

You can refer to the below screenshot to see the output for Check if Numpy array is Concatenate()

## Check if a value exists in a Numpy array

• Checking if a value exists in an array test if the array or any elements of the array contain the value:

Let’s take an example to test if num is present in a Numpy array, use num in arr which evaluates to true otherwise false

``````import Numpy as Np

num = 40
arr = Np.array([[1, 30],[4, 40]])
if num in arr:
print(True)
else:
print(False)
``````

You can refer to the below screenshot to see the output for Check if value exists in a Numpy array

This is how to check if a value exists in a Numpy array in Python.

## Check if the Numpy array is Nan in Python

• To check for Nan values in a Numpy array we can use the function Np.isNan(). The output array is true for the indices that are Nan in the original array and false for the rest.

Let’s take an example to check the Nan values in the given array

``````import Numpy as Np

num = 40
arr = Np.isNan([[1, 30],[4, 40]])
if num in arr:
print(True)
else:
print(False)``````

You can refer to the below screenshot to see the output for Check is Numpy array is Nan

The above Python code we can use to check if the Numpy array is Nan in Python.

## Check if Numpy array has Duplicates

To Check if the array contains any duplicate element or not . We use the method np.unique()

``````import numpy as np

a=np.array([1,2,2,2,2,3])

n = np.zeros_like(a, dtype=bool)
n[np.unique(a, return_index=True)] = True
a[~n]
print(n)``````

You can refer to the below screenshot to see the output for Check if Numpy array has Duplicates

This is how to check if Numpy array has duplicates in Python.

## Check if the NumPy array is 1d or 2d

One Dimensional Numpy array means the collection of homogenous data in a single row. It is also known as a vector. When it is a single row or 1D array you have to use only one square bracket.

Two Dimensional Numpy array means the collection of homogenous data in lists of a list. It is also known as a matrix. In 2D array, you have to use two square brackets that is why it said lists of lists.

Let us see, how to check if the Numpy array is 1d or 2d.

``````import numpy as np
b = np.array([2,5,6,7,1,7])
print(b)
print("Dimension:",b.ndim)
``````

You can refer to the below screenshot to see the output

This is how to check, if the numpy array is 1d or 2d in Python.

You may like the following Python tutorials:

In this Python tutorial, we learned how to check if NumPy Array is Empty in Python and cover the below examples:

• Check if Numpy Array is Empty
• How to check if Numpy Array is Numeric
• Check if Numpy Array is all zeroes
• How to check if Numpy Array is a Subset of another array
• Check if Numpy Array is all same value
• How to check if Numpy Array is a view
• Check if NumPy Array is Float
• How to check if Numpy Array is in list
• Check if Numpy Array is sorted
• Python Check if the Numpy array has negative values
• Check if Numpy array is Concatenate()
• How to check if a value exists in a Numpy array
• Check if the Numpy array is Nan in Python
• Check if Numpy array has Duplicates
• How to check if the NumPy array is 1d or 2d