Python - Check if a List is Empty

In Python you can check if a list is empty by using the built-in len() function. The len() function returns the number of items in a list, dictionary, string, tuple or set. If the length of a list is 0, then it's empty.

basket = []
if len(basket) == 0:
    print('Basket is empty')
# Basket is empty

bag = ['apple', 'banana', 'orange']
if len(bag) > 0:
    bag_count = len(bag)
    print('Bag contains ' + str(bag_count) + ' items')
# Bag contains 3 items