Iterator in For-loops

I just worked through the Conditional Statements lesson in the ‘Python for Data Science: Fundamentals’ course and in the Conditional Statements lesson (on step 9), they switch from using ‘row’ as the iterator to ‘app’ without explaining how they got away with it and despite never defining either term.
I’m a little confused as to how the computer knows what the programmer is instructing it to do when, apparently, they can use any term they want for the iterator in a For-loop.
You’ve defined what the iterable is (‘apps_data[1]:’ i.e. the lists of lists you created/defined earlier in the lesson, excluding the first row which is all headers)

Is the only executable piece of the For-loop syntax the iterable (defining what to loop over) and whatever gets included in the For-loop body (i.e. IF statements and ELSE/ELIF clauses)?
In that case, is the iterator just a non-executing term in the syntax that helps the programmer remember what the code is doing (and not interpreted for program execution)?

FYI: There’s another topic dedicated to For-loops (‘How does a For Loop Work?’) and here’s a quote from Mary’s (DataQuest Leader) response:
For-loop syntax:
for < iterator > in < iterable >
Here iterable is anything that is a collection of values. The best examples are a list, tuple, etc. iterator is a variable that stores each value in the iterable 1 at a time. You can name the iterator anything you want: row, app, pizza, etc. Just make sure that it is something meaningful considering the values in the iterable."

What’s referred to as <iterator> in Mary’s topic is just a variable, which holds the value of each item in the iterable as the for loop goes through it. When the for loop executes, it assigns the value of an item from the iterable, to a variable named <iterable>.

When you make a variable assignment:

my_var = "Hello!"

the computer stores the value "Hello!" somewhere, with the “name” my_var, and when you refer later to my_var, it knows to retrieve that specific value. When a for loop executes:

for app in apps_data[1]:
    # for loop body here

exactly the same thing happens. The computer stores one of the values from apps_data[1] into a variable called app.

The variable name is arbitrary, and decided by the programmer, just like any other variable name.

I hope this helps! :slightly_smiling_face:


On a related note (same step/lesson) … can you explain why, starting with step 9, the lesson’s code stops “initializing” an empty list or variable prior to starting the For-loop?

In the prior steps, the code always involved the “initializing” of an empty list or variable (i.e. “ratings = []” or “nonfree_games_social_ratings = []”) before starting the For-loop
For some reason, this step gets dropped as soon as the code starts using a different iterator name (“app” instead of "row’)

I think it might just need it as a temporary variable for the time being in the loop

This is the answer that i need, thank you very much. i was unable to understand why the iterator changes name everytime until now, i started to think that i didn’t get the whole for loop section

1 Like