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?
not a. Ifais, in fact, a numpy array with size 0, in recent versions of numpy that expression will generate a deprecation warning: DeprecationWarning: The truth value of an empty array is ambiguous. Returning False, but in future this will result in an error. Usearray.size > 0to check that an array is not empty. And that messages suggests that checking thesizeattribute is the recommended method.ais a list such asa = [[], [], []], thennot awill be False (becauselen(a)is 3), butnp.array(a).sizeis 0 (because the array that is created has shape (3, 0)).