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 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