How to check whether a NumPy array is empty or not? (2024)

  • Home
  • DS & Algo. ▾
    • Data Structure
    • Algorithms
    • Coding Problems
  • Languages ▾
    • C
    • C++
    • C++ STL
    • Java
    • Python
    • Scala
    • Ruby
    • C#.Net
    • Golang
    • Android
    • Kotlin
    • SQL
  • Web. ▾
    • JavaScript
    • CSS
    • jQuery
    • PHP
    • Node.Js
    • AdonisJs
    • VueJS
    • Ajax
    • HTML
    • Django
  • Programs ▾
    • C
    • C++
    • Data Structure
    • Java
    • C#.Net
    • VB.Net
    • Python
    • PHP
    • Golang
    • Scala
    • Swift
    • Rust
    • Ruby
    • Kotlin
    • C Interview Programs
  • Aptitude ▾
    • C Aptitude
    • C++ Aptitude
    • Java Aptitude
    • C# Aptitude
    • PHP Aptitude
    • Linux Aptitude
    • DBMS Aptitude
    • Networking Aptitude
    • AI Aptitude
    • More...
  • Interview ▾
    • Golang
    • MIS Executive
    • DBMS
    • C
    • Embedded C
    • Java
    • SEO
    • HR
  • Find Output ▾
    • C
    • C++
    • C#.Net
    • Java
    • Go
    • PHP
    • More...
  • MCQs ▾
    • Web Technologie MCQs
    • CS Subjects MCQs
    • Databases MCQs
    • Programming MCQs
    • Testing Software MCQs
    • Digital Mktg Subjects MCQs
    • Cloud Computing S/W MCQs
    • Engineering Subjects MCQs
    • Commerce MCQs
    • More MCQs...
  • CS Subjects ▾
    • Machine Learning/AI
    • Operating System
    • Computer Network
    • Software Engineering
    • Discrete Mathematics
    • Digital Electronics
    • Data Mining
    • MIS
    • DBMS
    • Embedded Systems
    • Cryptography
    • CS Fundamental
    • More Tutorials...
  • More ▾
    • Tech Articles
    • Puzzles
    • Full Forms
    • Code Examples
    • Blogs
    • Guest Post
    • Programmer's Calculator
    • XML Sitemap Generator
    • About
    • Contact

Home »Python »Python Programs

Check Empty NumPy Array: In this tutorial, we will learn how to check whether a NumPy array is empty or not using different methods?By Pranit Sharma Last updated : May 26, 2023

Problem Statement

Given a NumPy array, we have to check whether it is an empty array or not.

Checking whether a NumPy array is empty or not

To check an empty NumPy array, there are multiple methods that you can use such as numpy.ndarray.size attribute, numpy.any() method, and numpy.size() method. Let's discuss all these methods with examples.

1) Using numpy.ndarray.size Attribute

The ndarray.size attribute returns the total number of elements in a NumPy array. If the given array is empty, it returns 0; the Number of elements, otherwise.

Syntax

ndarray.size

Example 1:

Consider the below Python program to check NumPy array is empty or not using ndarray.size attribute.

# Import NumPyimport numpy as np# Creating empty and non-empty arraysarr1 = np.array([])arr2 = np.array([10, 20, 30])# Printing original arraysprint("arr1:\n", arr1, "\n")print("arr2:\n", arr2, "\n")# Check whether array is empty or not# using ndarray.size attributeif arr1.size == 0: print("arr1 is an empty array")else: print("arr1 is not an empty array")if arr2.size == 0: print("arr2 is an empty array")else: print("arr2 is not an empty array")

Output

arr1: [] arr2: [10 20 30] arr1 is an empty arrayarr2 is not an empty array

2) Using numpy.any() Method

The numpy.any() method checks whether any array element along a given axis evaluates to True. If the given array is not empty it returns True; False, otherwise.

Syntax

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

Example 2

Consider the below Python program to check NumPy array is empty or not using numpy.any() method.

# Import NumPyimport numpy as np# Creating empty and non-empty arraysarr1 = np.array([])arr2 = np.array([10, 20, 30])# Printing original arraysprint("arr1:\n", arr1, "\n")print("arr2:\n", arr2, "\n")# Check whether array is empty or not# using numpy.any() methodif np.any(arr1) == False: print("arr1 is an empty array")else: print("arr1 is not an empty array")if np.any(arr2) == False: print("arr2 is an empty array")else: print("arr2 is not an empty array")

Output

arr1: [] arr2: [10 20 30] arr1 is an empty arrayarr2 is not an empty array

3) Using numpy.size() Method

The numpy.size() method returns the total number of elements in an array along a given axis (optional). If the given array is empty, it returns 0; the Number of elements, otherwise.

Syntax

numpy.size(arr, axis=None)

Example 3

Consider the below Python program to check NumPy array is empty or not using numpy.size() method.

# Import NumPyimport numpy as np# Creating empty and non-empty arraysarr1 = np.array([])arr2 = np.array([10, 20, 30])# Printing original arraysprint("arr1:\n", arr1, "\n")print("arr2:\n", arr2, "\n")# Check whether array is empty or not# using numpy.size() methodif np.size(arr1) == 0: print("arr1 is an empty array")else: print("arr1 is not an empty array")if np.size(arr2) == 0: print("arr2 is an empty array")else: print("arr2 is not an empty array")

Output

arr1: [] arr2: [10 20 30] arr1 is an empty arrayarr2 is not an empty array

Python NumPy Programs »

Find Unique Rows in a NumPy Array

Replace all elements of NumPy array that are greater than some value

Related Tutorials

  • Convert array of indices to one-hot encoded array in NumPy?
  • How to Create NumPy Matrix Filled with NaNs?
  • NumPy Matrix of All True or All False
  • How to Transpose a 1D NumPy Array?
  • NumPy Array: Moving Average or Running Mean
  • How to calculate percentiles in NumPy?
  • Is it possible to use numpy.argsort() in descending order?
  • How to Convert List of Lists to NumPy Array?
  • How to add a new row to an empty NumPy array?
  • Extract Specific Columns in NumPy Array (3 Best Ways)
  • How to Get Random Set of Rows from 2D NumPy Array?
  • 'Cloning' Row or Column Vectors to Matrix
  • How to flatten only some dimensions of a NumPy array?
  • Difference Between NumPy's mean() and average() Methods
  • Concatenate a NumPy array to another NumPy array
  • How to split data into 3 sets (train, validation and test)?
  • How to count the number of true elements in a NumPy bool array?
  • Add row to a NumPy array
  • How do you get the magnitude of a vector in NumPy?
  • What is the inverse function of zip?

Comments and Discussions!

Load comments ↻


How to check whether a NumPy array is empty or not? (2024)
Top Articles
Latest Posts
Article information

Author: Msgr. Refugio Daniel

Last Updated:

Views: 5352

Rating: 4.3 / 5 (54 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Msgr. Refugio Daniel

Birthday: 1999-09-15

Address: 8416 Beatty Center, Derekfort, VA 72092-0500

Phone: +6838967160603

Job: Mining Executive

Hobby: Woodworking, Knitting, Fishing, Coffee roasting, Kayaking, Horseback riding, Kite flying

Introduction: My name is Msgr. Refugio Daniel, I am a fine, precious, encouraging, calm, glamorous, vivacious, friendly person who loves writing and wants to share my knowledge and understanding with you.