Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a list is empty?

Tags:

python

list

For example, if passed the following:

a = [] 

How do I check to see if a is empty?

like image 956
Ray Avatar asked Sep 10 '08 06:09

Ray


People also ask

How do you check if a list has an empty string?

You can just check if there is the element '' in the list: if '' in lst: # ... if '' in lst[:1]: # for your example # ...

How do you check if an element is empty in Python?

Checking empty list using len() Function. The len() function is used to find the number of elements in the list. So, to check if the list is empty or not using len(), we can pass the empty list to the len() function, and if we get 0, that means the list is empty.

Is Empty function for list?

list::empty() is an inbuilt function in C++ STL which is declared in header file. list::empty() checks whether the given list container is empty(size is 0) or not, and returns true value if the list is empty and false if the list is not empty.

How do I check if an array is 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

if not a:   print("List is empty") 

Using the implicit booleanness of the empty list is quite pythonic.

like image 171
Patrick Avatar answered Sep 21 '22 06:09

Patrick