To check if a list is empty in Python, you can use the len() function or directly check the list as a boolean expression.
In this example,
my_list
.len()
function to get the length of my_list
.my_list = []
if len(my_list) == 0:
print('List is empty')
else:
print('List is not empty')
List is empty
In this example,
my_list
.my_list
is empty as a boolean expression.my_list
is empty, the boolean expression evaluates to False
; otherwise, it evaluates to True
.my_list = []
if not my_list:
print('List is empty')
else:
print('List is not empty')
List is empty
In this tutorial, we learned How to check if a List is Empty in Python language with well detailed examples.