Simplifying of for statment

Feature or enhancement


Like any other language Python has loop statement for.
Now it has following notation
for item in list_of_items:
But what if we can add notation like this
for list_of_items:
and instead of using item we reserve a some word for this pusrpose, like an option we can use the name of the list just removing the last letter ‘s’.
Examples :

for cats:
for dogs:

I don’t think this change improves the language. Is there some specific use case you’re thinking of where this makes the code better?


A reserved word won’t work in nested loops:

for cats:
    for dogs:
        print(???, ???)

And automatic depluralization, even if you restrict it to a single language, would be nearly impossible:

for person in people:

for entity in entities:

for analysis in analyses:

Plus, the existing for loop is able to do all kinds of assignments:

for key, value in some_dict.items():

There’s very little benefit in defining the iteration variable implicitly, and that isn’t the way Python is designed.


What about the word ‘species’?

Clearly “specy”. Someone want to write to all the dictionaries and get it added?

Use uncountable nouns for infinite generators!

1 Like

I don’t suggest to completely change a for loop notation, but make an additional option

for cats:
for dogs:
print(Reserved_word[0], Reserved_word[0])

And automatic depluralization, even if you restrict it to a single language, would be nearly impossible:

I was just a thought? but instead of depluarization you can just ad underscore to name

for people:

for cats:
    for dogs:
        print(_cats, _dogs)

for people is equivalent to using map(people, func) . What is really happening in the Python for loop is that each individ in people is treated individually. Hence, for individ in people makes sense.

for cats:
    for dogs:
        print(Reserved_word[0], Reserved_word[0])

This seems dramatically less clear and obvious than:

for cat in cats:
    for dog in dogs:
        print(cat, dog)

instead of depluarization you can just ad underscore to name

There is no “implicitly conjure a new variable” scheme that will not run the risk of conflicting with user code.

1 Like

Yeah…saving a few characters doesn’t make code simpler, it just makes it shorter. That’s not the same thing.


The only way for this proposal to make any sense is to hide a throwaway variable, typically the unsightly _, which always only serves to confuse newbies:

for range(5):

I would be for the proposal in this case.

FWIW, I wouldn’t. Explicit is better than implicit, and all that.

Is is really worth implementing a disruptive change to the language in order to achieve that small gain? Early on, Python beginners are typically taught that the name of a variable can begin with a letter or an underscore. Equipped with that knowledge, they can also easily be taught that _ is a valid name for a variable, and that its low-profile nature signifies that the variable’s value is of small significance. In fact, I remember seeing that for the first time as a beginner, and thinking that it was cool.

Still better than a non-visible variable, though.

I appreciate that you recognize the change as a gain, but I disagree that it is disruptive. It is unambiguous, backwards-compatible (does not introduce a new keyword and currently produces a SyntaxError) and does not require a big change to the existing code base. Hardly disruptive.

The point of my proposal is that it is intuitive, less noisy and therefore enhances readability.

Just because someone can be taught to learn something counter-intuititve and get used to it does not mean that something can’t be improved.

Coolness should not be a factor in the design of a programming language focused on readability.

OK, when I said “hide” I really actually meant “abolish”. The for iterable: syntax should not perform any assignment at all. That would make it perfectly explicit.

See above.

You don’t avoid an assignment somewhere unless you can somehow avoid keeping track of the loop entirely. It might be internal but it’s still there.

Neither should “unsightliness” be a factor. But readability should, and for [literal]: is ungrammatical and less readable than the status quo.

Of course I was talking about “avoiding assignment” in terms of a Python variable. Of course the loop is kept track of in the implementation.

Perhaps a new keyword can be introduced if people find for ungrammatical for this purpose then.

But that’s irrelevant here. The loop iterator is still going to get pumped even if you don’t do anything with it. Consider a classic for loop:

# for item in stuff:
_iter = iter(stuff)
while True:
        item = next(_iter)
    except StopIteration:
    ... # loop body goes here

If you remove the item = from the line that calls next(), that doesn’t change anything about the iteration. But that part is entirely hidden already, being part of the loop mechanism itself.

Had Python always supported for stuff: in this way (no assignment, just discard the iteration values), it would seem perfectly reasonable. The question is, is it worth adding? Personally, I think the benefit is extremely marginal (compared to assigning to _), so it doesn’t justify the complexity, but I’m open to the argument that the value is greater than I’ve been seeing.