Are you checking the list correctly?

Python’s built-in len() function can be used to determine whether or not a list is empty. If the length of the list is 0, it means that the list contains no elements. …

Updated October 5, 2023

Python’s built-in len() function can be used to determine whether or not a list is empty. If the length of the list is 0, it means that the list contains no elements.

my_list = []
if len(my_list) == 0:
    print("The list is empty!")
else:
    print("The list has elements.")

If you run this code, “The list is empty!” will be printed because my_list is an empty list.

This method can be useful in various contexts such as checking if a user input is empty (like when prompting for a username), if there are no items to display on a website, and so forth.

Hey! Do you love Python? Want to learn more about it?
Let's connect on Twitter or LinkedIn. I talk about this stuff all the time!