Python Program to Check If a List is Empty

 

Example 1: Using Boolean operation

my_list = []

if not my_list:

    print("the list is empty")

Output

the list is empty

It is the most pythonic way of testing the emptiness. If you want to learn more about boolean truth value, you can refer to Truth Value Testing.


Example 2: Using len()

my_list = []

if not len(my_list):

    print("the list is empty")

Output

the list is empty

If the length of a list is 0, then the list is empty.


Example 3: Comparing with []

my_list = []

if my_list == []:

    print("The list is empty")

Output

The list is empty

 

0 Comments