103

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

if not self.table[5] is None:

Is this the right way?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
amchew
  • 1,167
  • 3
  • 8
  • 6

8 Answers8

107

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"
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • 3
    This method is dangerous because, e.g., bool(numpy.array([0])) evaluates to False. Remi's test using a.size is correct. – Drew Frank Jun 04 '12 at 18:47
  • Yeah, this is wrong and should not have so many upvotes – endolith Dec 16 '12 at 17:11
  • 6
    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. – chthonicdaemon Jan 02 '14 at 07:07
  • 8
    @DrewFrank, this is the right answer to the question. You are __inventing__ the question and then claim that the answer is wrong – volcano Jan 02 '14 at 07:09
  • @volcano seems not, the OP clearly specified an `array`, not a `list`... – gt6989b May 19 '14 at 20:19
  • 2
    @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. – John Kugelman May 19 '14 at 20:48
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.)

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Remi
  • 20,619
  • 8
  • 57
  • 41
  • Using numpy just to check if a list is empty seems heavy handed. – Pengo May 16 '17 at 15:10
  • 6
    @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. – Remi May 16 '17 at 18:59
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.

bcho04
  • 538
  • 1
  • 7
  • 11
8
if self.table:
    print 'It is not empty'

Is fine too

Senthil Kumaran
  • 54,681
  • 14
  • 94
  • 131
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.

Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
superDoge
  • 71
  • 1
  • 5
4

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')
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
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()
Avizipi
  • 502
  • 6
  • 16
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")
Bonifacio2
  • 3,405
  • 6
  • 34
  • 54