Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Python Basics (2015) Shopping List App Continue

Didier Borel
Didier Borel
2,837 Points

Item vs items

As i am being asked to print out every item in the list "items" I don't understand why the correct code is print(item) and not print (items).if iprint item, won't I just get 1 item of the list?

breaks.py
def loopy(items):
    # Code goes here

2 Answers

Hi Didier

Items is an iterable meaning you can loop through it. If my basket called items has 6 fruits ["apples","bananas","grapes","oranges","strawberries","watermelon"], and i wanted to print out each fruit I would say like below

for item in items:
    print(item)

# the output would be 

apples
bananas
grapes
oranges
strawberries
watermelon

hope this helps

Didier Borel
Didier Borel
2,837 Points

Andreas, thxs for your answer. I will take it as just the way it is. Even though it seems illogical. If I am going to loop through many things, it seems more logical to say print (items), i imagine print(item) will only print 1 item. And is items some reserved name? What if I said print (object) would i get anything.?

Hi Didier

Depends what you are trying to achieve. Yes you can print items which will return a list like so ['apples' 'bananas','grapes','oranges','strawberries','watermelon']. But what if i said to you Didier only print out fruits that are less than 8 characters, that's where loops comes in when you need to evaluate each item in a list.

for item in items:
  if len(item)< 8:
    print(item)
  else:
    continue

# my experience you will almost always have to loops around lists to evaluate each item or row. 

Hope this helps