105

How to check if the array is not empty? I did this:

if not self.table[5] is None:

Is this the right way?

1
  • 1
    @birryree: Not a duplicate. bool([]) == bool(array([])), but bool([0]) != bool(array([0])). Commented Jan 5, 2013 at 19:44

8 Answers 8

111

There's no mention of numpy in the question. If by array you mean list, then if you treat a list as a boolean it will yield True if it has items and False if it's empty.

l = []

if l:
    print "list has items"

if not l:
    print "list is empty"
Sign up to request clarification or add additional context in comments.

6 Comments

This method is dangerous because, e.g., bool(numpy.array([0])) evaluates to False. Remi's test using a.size is correct.
Yeah, this is wrong and should not have so many upvotes
This answer is correct when working with lists. a.size will not work if a is a list. The OP should have been more specific about the datatype.
@DrewFrank, this is the right answer to the question. You are inventing the question and then claim that the answer is wrong
@gt6989b By clearly do you mean not clearly? Without clarification, one could presume that the OP was asking about lists but used the wrong word. My answer spells out that presumption quite explicitly. I guessed that the OP was more likely asking about lists than about numpy arrays.
|
64

with a as a numpy array, use:

if a.size:
   print('array is not empty')

(in Python, objects like [1,2,3] are called lists, not arrays.)

2 Comments

Using numpy just to check if a list is empty seems heavy handed.
@Pengo: I did not suggest to convert a list to a numpy array. This answer assumes that 'a' already is a numpy array. Talking about 'arrays' in Python quickly refers to numpy arrays, because natively Python does not have Array objects, just Lists.
10

len(self.table) checks for the length of the array, so you can use if-statements to find out if the length of the list is greater than 0 (not empty):

Python 2:

if len(self.table) > 0:
    #Do code here

Python 3:

if(len(self.table) > 0):
    #Do code here

It's also possible to use

if self.table:
    #Execute if self.table is not empty
else:
    #Execute if self.table is empty

to see if the list is not empty.

Comments

8
if self.table:
    print 'It is not empty'

Is fine too

1 Comment

In case of empty array: if not arr.shape: print("array is empty!)
7
print(len(a_list))

As many languages have the len() function, in Python this would work for your question.

If the output is not 0, the list is not empty.

1 Comment

worked using array('d') from the array package.
5

An easy way is to use Boolean expressions:

if not self.table[5]:
    print('list is empty')
else:
    print('list is not empty')

Or you can use another Boolean expression :

if self.table[5] == []:
    print('list is empty')
else:
    print('list is not empty')

Comments

4

If you are talking about Python's actual array (available through import array from array), then the principle of least astonishment applies and you can check whether it is empty the same way you'd check if a list is empty.

from array import array
an_array = array('i') # an array of ints

if an_array:
    print("this won't be printed")

an_array.append(3)

if an_array:
    print("this will be printed")

Comments

4

I can't comment yet, but it should be mentioned that if you use numpy array with more than one element this will fail:

if l:
    print "list has items"

elif not l:
    print "list is empty"

the error will be:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.