How to check if array is not empty?

Python

Python Problem Overview


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

if not self.table[5] is None:

Is this the right way?

Python Solutions


Solution 1 - Python

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"

Solution 2 - Python

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.)

Solution 3 - Python

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.

Solution 4 - Python

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

Is fine too

Solution 5 - Python

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.

Solution 6 - Python

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')

Solution 7 - Python

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()

Solution 8 - Python

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")



Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionamchewView Question on Stackoverflow
Solution 1 - PythonJohn KugelmanView Answer on Stackoverflow
Solution 2 - PythonRemiView Answer on Stackoverflow
Solution 3 - Pythonbcho04View Answer on Stackoverflow
Solution 4 - PythonSenthil KumaranView Answer on Stackoverflow
Solution 5 - PythonsuperDogeView Answer on Stackoverflow
Solution 6 - PythonMrGeekView Answer on Stackoverflow
Solution 7 - PythonAssaf LivneView Answer on Stackoverflow
Solution 8 - PythonBonifacio2View Answer on Stackoverflow