Driver FixRecommendedSound, Wi-Fi or graphics acting up? Check drivers firstFind missing or outdated drivers fast.Check DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk1 min

How to Check if a NumPy Array Is Empty in Python (size vs len)

Check a NumPy array for zero total elements with arr.size == 0. Learn how that differs from len(arr), which reports the first dimension's length.

What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use arr.size == 0 to check whether a NumPy array contains zero elements. Unlike len(arr), arr.size counts elements across every dimension.

Check whether the array contains any elements

ndarray.size is the total number of elements in a NumPy array. Test it against zero:

if arr.size == 0:
    print("array has no elements")

This checks element count, not the values stored in the array. An array filled with numeric zeros is not empty if it has elements.

What is the difference between size and len?

arr.size counts every element. For an ndarray, len(arr) gives the length of the first dimension. In one dimension these counts match; in multiple dimensions they can differ.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import numpy as np

one_d = np.array([])
print(one_d.size == 0)  # True
print(len(one_d) == 0)  # True

zero_columns = np.empty((3, 0))
print(zero_columns.size == 0)  # True
print(len(zero_columns) == 0)  # False: first dimension has length 3

The second array has shape (3, 0): its first dimension has length three, but its second dimension has length zero, so it contains no elements. Use arr.size == 0 when you mean that the entire array has zero elements.

How do shapes and scalar arrays affect the check?

Any shape with a zero-length dimension has zero total elements. For example, (0,), (0, 4), and (3, 0) all satisfy arr.size == 0. By contrast, a zero-dimensional array is scalar-shaped and can contain one element. The number of dimensions, shape, and total element count describe different properties; size checks the last one.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

When should you use len or another property?

  • Use arr.size == 0 to test for zero total elements, including in multidimensional arrays.
  • Use len(arr) when you specifically need the length of the first dimension.
  • Use arr.nbytes when you need the array’s memory consumption in bytes; arr.size is a count of elements, not bytes.

The size attribute applies to NumPy arrays. If your input may instead be a Python list or another object, decide whether to convert it to an array before using this check.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Leave a Reply

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

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Outdated Drivers Are Slowing You DownFree scan - exact matches

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.