1

How do I test if a numpy array is empty. I have problem if the there is only one element with value 0. If I do

a = np.array([0])
a.any()

results is False while

a = np.array([1])
a.any()

is True So how do I check np.array has no elements?

6
  • 1
    Check if a.size == 0 should do the trick Commented May 19, 2020 at 4:46
  • at the moment I do len(a) == 1 to capture the only element with value 0. Better perhaps to use a.size != 0 Commented May 19, 2020 at 4:55
  • Checking the size makes most sense. But why why do you need to check this? Is this a 1d array? How is it created? Commented May 19, 2020 at 5:48
  • The future warning produced by bool(np.array([])) suggests using array.size > 0. Commented May 19, 2020 at 7:27
  • The array contains a list of index numbers where a value is duplicate in another list. If the array is not empty then I remove these elements. This worked all fine until I came across one where the index value was zero. Commented May 19, 2020 at 7:39

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.