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) Python Data Types Index

a a
a a
1,101 Points

code challenge question

I have trouble in shopping list challenge. https://teamtreehouse.com/library/python-basics/shopping-list-app/continue So I make a simple code installing anaconda to make it clear and I have a question now.

items = ["abc", "xyz"] for item in items: print(item) abc xyz

items[0] "abc"

item[0] "x"

why item[0] = "x"? I thought item[0] = "a".

You are you confused item with items, item is an iterator end up to be 'xyz' . in the last loop, and item[0] == 'x' you can replace all 'item' to 'individual_string' to be clear.

1 Answer

Steven Parker
Steven Parker
231,248 Points

So item[0] would be "a" during the first pass through the loop.

But on the second (and last) pass, "item" was set to "xyz". So at that point, item[0] is "x".