Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Python Collections (2016, retired 2019) Lists Shopping List Take Three

Hsieh Gardner
Hsieh Gardner
2,599 Points

I don't understand the:"if len(shopping_list):"

def add_to_list(item): show_list() if len(shopping_list): position = input("Where should I add {}? \n" "press enter to the end of the list \n" "> ".format(item))

WHY is "len(shopping_list):"

1 Answer

Ray Karyshyn
Ray Karyshyn
13,443 Points

Hi Hsieh,

What the program is doing is checking if there is something in shopping_list. If the length of the list is greater than zero, it essentially returns true, and false if the length is zero.

shopping_list = []

if len(shopping_list):
    print("aw")  # <- this is what is printed since there is nothing in 'shopping_list'

The programmer is utilizing this because if there is nothing in the list then the item is just added. However, if there is one or more items, then the user can choose where is put the new item.