oaklander114 oaklander114 - 1 year ago 59
Python Question

What do you call the item of list when used as an iterator in a for loop?

I'm not sure how you name the

in the following
loop. Is there are a term for it?

for n in [1,2,3,4,5]:
print i

And, am I correct that the list itself is the
of the
loop ?

Answer Source

The example you gave is an "iterator-based for-loop"

n is called the loop variable.

The role that list plays is more troublesome to name.

Indeed, after an interesting conversation with @juanpa.arrivillaga I've concluded that there simply isn't a "clearly correct formal name", nor a commonly used name, for that syntactic element.

That being said, I do think that if you referred to it in context in a sentence as "the loop iterator" everyone would know what you meant.

In doing so, you take the risk of confusing yourself or someone else with the fact that the syntactic element in that position is not in fact an iterator, its a collection or (loosely, but from the definition in the referenced article) an "iterable of some sort".

I suspect that one reason why there isn't a name for this is that we hardly ever have to refer to it in a sentence. Another is that they types of element that can appear in that position vary widely, so it is hard to safely cover them all with a label.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download