To check if a list is not empty in Python, you can use the implicit boolean evaluation of the list.
In this example,
my_list
with some elements.my_list
is not empty. In Python, an empty list evaluates to False
, and a non-empty list evaluates to True
.my_list = [1, 2, 3]
if my_list:
print('The list is not empty')
else:
print('The list is empty')
The list is not empty
In this example,
empty_list
without any elements.empty_list
is not empty.empty_list = []
if empty_list:
print('The list is not empty')
else:
print('The list is empty')
The list is empty
In this tutorial, we learned How to check if a List is Not Empty in Python language with well detailed examples.