Home > Software engineering >  Check if numpy's array_like is an empty array
Check if numpy's array_like is an empty array

Time:01-08

Suppose a is an array_like and we want to check if it is empty. Two possible ways to accomplish this are:

if not a:
   pass

if numpy.array(a).size == 0:
   pass

The first solution would also evaluate to True if a=None. However I would like to only check for an empty array_like.

The second solution seems good enough for that. I was just wondering if there is a numpy built-in function for that or a better solution then to check for the size?

CodePudding user response:

If you want to check if size is zero, you might use numpy.size function to get more concise code

import numpy
a = []
b = [1,2]
c = [[1,2],[3,4]]
print(numpy.size(a) == 0)  # True
print(numpy.size(b) == 0)  # False
print(numpy.size(c) == 0)  # False
  • Related