Skip to content
  • (+91) 9409548155
  • support@appdividend.com
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Menu
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
  • Home
  • Pricing
  • Instructor
  • Tutorials
    • Laravel
    • Python
    • React
    • Javascript
    • Angular
  • Become A Tutor
  • About Us
  • Contact Us
Python

How to Create and Check If a Numpy Array is Empty

  • 02 Apr, 2025
  • Com 0
How to create and check an empty numpy array in Python

In numpy, “empty” is an ambiguous term because an empty array means an array with zero elements or an uninitialized array with arbitrary memory content. It can have various shapes (0,), (0, n), or (m, 0, n)—as long as its total number of elements is zero.

Empty and Non-empty array visual representationCreating an empty array

The optimal method for creating a one-dimensional empty array is to use the np.array() function.

For a multidimensional empty array, the np.empty() function is more efficient.

Using np.array()

You can directly initialize an array with zero elements like this:

creating an empty numpy array

import numpy as np

empty_array = np.array([])

print(empty_array)

print(empty_array.shape)

print(empty_array.size)

# Output:
# []
# (0,)
# 0

The output is a one-dimensional array with no values. Its shape is (0,). This approach is direct and readable.

Using np.empty()

creating a 2d empty numpy arrayFor creating a 2D or multidimensional empty array, the better approach is to use the “np.empty()” function.

Let’s define rows and columns for a 2D array:

import numpy as np

arr2d = np.empty((0, 3)) # 0 rows and 3 columns

print(arr2d)

print(arr2d.size)

print(arr2d.shape)

# Output:
# []
# 0
# (0, 3)

It is essential to note that the np.empty() method sometimes fills the rows with zeros, while at other times it does not fill the array with zeros but with arbitrary uninitialized values, depending on the operating system.

import numpy as np

init_2d = np.empty((2, 3)) # 2 rows 3 columns

print(init_2d)

print(init_2d.size)

print(init_2d.shape)

# Output:

# [[0. 0. 0.]
#  [0. 0. 0.]]

# 6

# (2, 3)

For my macOS, it creates two rows and three columns of 0s. Your output might be different.

Let’s initialize a 3D empty array:

import numpy as np

arr3d = np.empty((2, 0, 4)) # 2 rows 0 columns 4 depth

print(arr3d)

print(arr3d.size)

print(arr3d.shape)

# Output:
# []
# 0
# (2, 0, 4)

Checking if a numpy array is empty

The most Pythonic way to check if an array is empty is to use the .size attribute. It returns the total number of elements in the array. If the size is 0, the array is empty; otherwise, it is not.

Using numpy.ndarray.size attribute to check if a numpy array is empty

For 1D array

import numpy as np

empty_array = np.array([])

if empty_array.size == 0:
    print("Array is empty")
else:
    print("Array is not empty")

Output

Array is empty

For 2D array

To check its emptiness, we can use the .size attribute on it.

import numpy as np

# Empty 2D array
empty_2d_array = np.empty((0, 5))  # 0 rows, 5 columns
print(f"Empty 2D array:\n{empty_2d_array}")
if empty_2d_array.size == 0:
    print("The 2D array is empty (using .size)")

# Non-empty 2D array
non_empty_2d_array = np.array([[1, 2, 3], [4, 5, 6]])
print(f"\nNon-empty 2D array:\n{non_empty_2d_array}")
if non_empty_2d_array.size == 0:
    print("The 2D array is empty (using .size)")
else:
    print("The 2D array is NOT empty (using .size)")

Output

Empty 2D array:
[]
The 2D array is empty (using .size)

Non-empty 2D array:
[[1 2 3]
 [4 5 6]]
The 2D array is NOT empty (using .size)

It is the most efficient and straightforward way.

Post Views: 15
Share on:
Krunal Lathiya

With a career spanning over eight years in the field of Computer Science, Krunal’s expertise is rooted in a solid foundation of hands-on experience, complemented by a continuous pursuit of knowledge.

How to Create and Check Empty Dictionary in Python
How to Check If a Tuple Contains an Element in Python

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Address: TwinStar, South Block – 1202, 150 Ft Ring Road, Nr. Nana Mauva Circle, Rajkot(360005), Gujarat, India

Call: (+91) 9409548155

Email: support@appdividend.com

Online Platform

  • Pricing
  • Instructors
  • FAQ
  • Refund Policy
  • Support

Links

  • About Us
  • Contact Us
  • Privacy Policy
  • Terms of services

Tutorials

  • Angular
  • React
  • Python
  • Laravel
  • Javascript
Copyright @2024 AppDividend. All Rights Reserved
Appdividend