Python - Check if a List is Empty

1 min read

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

Author picture
Rhett Trickett

Founder at Able

@rhett RhettTrickett rhetttrickett

Join the discussion

Able is a developer community where people build their coding knowledge and careers.

Join with GitHub Join with Twitter