my_list = list()
# Check if a list is empty by its length
if len(my_list) == 0:
pass # the list is empty
# Check if a list is empty by direct comparison (only works for lists)
if my_list == []:
pass # the list is empty
# Check if a list is empty by its type flexibility **preferred method**
if not my_list:
pass # the list is empty
a = []
if not a:
print("List is empty")
if len(li) == 0:
print('the list is empty')
>>> a = []
>>> not a
True
How to check for empty array in python
code
# empty list & non-empty list
empty_list = []
non_empty_list = [1, 2, 3, 4]
# check if list is empty
def check_list_empty(lst):
if len(lst) == 0:
print('The List is empty')
else:
print('The list is not empty')
# pass in the lists to check_list_empty
check_list_empty(empty_list)
check_list_empty(non_empty_list)
#Output
The list is empty
The List is not empty