Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if array is not empty? [duplicate]

Tags:

python

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

if not self.table[5] is None: 

Is this the right way?

like image 729
amchew Avatar asked Feb 23 '11 01:02

amchew


People also ask

How do I check if an array has duplicates?

To check if an array contains duplicates: Use the Array. some() method to iterate over the array. Check if the index of the first occurrence of the current value is NOT equal to the index of its last occurrence. If the condition is met, then the array contains duplicates.

How do you check if an array is not empty?

The array can be checked if it is empty by using the array. length property. This property returns the number of elements in the array. If the number is greater than 0, it evaluates to true.

How do you check if an array is not empty in Python?

To check if an array is empty in Python, use the numpy. ndarray. size property and compare it with zero(0). If the number is 0, then an array is empty.


1 Answers

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" 
like image 188
John Kugelman Avatar answered Sep 19 '22 13:09

John Kugelman