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 Introducing Lists Using Lists Mutability

In For loops ... where do the parameters come from - like item in inventory ... where does "item" come from?

... hopefully my English is good enough to describe my question ... in the REPL ... Craig writes ... item in inventory ... sure the list contains items, but how can it be processed ... I did not understand this before, but there it was attendee in attendees ... and the string attendee is part of attendees, or wish is part of wishes, but item is not in inventory ... hope this makes sense ... I already didn't know how to formulate the question, in order to be understood ... maybe I should rewatch the video ... helped several times ...

I just got my answer in the chapter multidimensional list ...

2 Answers

for loops in any language loops in a similar format. It contains 3 constructs...

  • start_parameter(initializer)
  • incrementer
  • end_parameter(loop runs till this condition is true)

in Python:

for i in range(0, 10):
    print(i)

# will give all the numbers from o to 9. Here staring was 0, end was 10(anything less than 10 is fine) and finally incrementer is 1(by default) 
for i in range(0, 10, 3):
    print(i)

# will give all the numbers 0, 3, 6, 9. Here staring was 0, end was 10(anything less than 10 is fine) and finally incrementer is 3(last parameter)

For a list... well, range(0, x) will return a range class but you can imagine it like a list.

for item in [10, 2, 3, 4, 5]:
    print(i)

#Will return 10, 2, 3, 4, 5. i.e, elements of list one by one. here start is 10, end is 5, and incrementer is not a number but an iterator that is incremeting by 1.

So when you say..

for item in inventory:

# item refers to an element in the inventory

you might be getting confused with:

if "item" in "inventory":

# or

if "attendee" in "attendees":

Here it is in is checking if first string("items" or "attendee") is present in second string.

in checks for membership.

so in case of for loops it checks for elements in list.

and for strings as well, it does the same operation. Since strings can be treated as list("item"[0]="i", "item"[1]="t","item"[2]="e", "item"[3]="m").

Hope this helps.

thank you for your answer Krishna ... the confusion derived from ... I got stuck ... because I thought, it must have been declared somewhere before ... and I just didn't get it ... but it is declared right then ... but your explanation is very good ... especially, when you mentioned the "i" ... which also isn't "declared" at some point before ... the "i" cleared it up even m ore ... thank you a lot ... Jan.