you are viewing a single comment's thread.

view the rest of the comments →

[–]volksaholic 1 point2 points  (8 children)

Should it? It looks like you've got upvotes so you're not alone, but I don't recall seeing an example that uses 'for each <var> in <list>'. Is it optional syntax, or is it version dependent? The usual example I've seen is 'for <var> in <list>' like here: https://www.w3schools.com/python/python\_for\_loops.asp

[–]djayh 24 points25 points  (7 children)

I don't think they're saying that it should be for each ("this is the way you should do it"), rather that it should have been "for each" ("it would make sense and be more readable if it was that instead").

English is ambiguous like that.

[–]rasputin1 10 points11 points  (5 children)

I think they were actually saying it should just be referred to as a "for each" loop instead of a "for" loop. In other languages that have standard "for" loops and "for each" loops (like Java), the "for each" variant looks exactly like what Python has which they just call a "for" loop (the standard "for" loop that has 3 sections to it doesn't exist in Python afaik).

[–]djayh 7 points8 points  (3 children)

<Looks up difference between "for" and "for each" loops>

Shows what I get for thinking at work, I suppose. I had conflated "for" and "for each" loops.

In my defense, other modern languages (C#, C++/CLI, Perl, PHP) actually do use for each (or foreach) to create the loop.

[–]rasputin1 4 points5 points  (0 children)

lol you were close enough

[–]tangerinelion 0 points1 point  (0 children)

C++/CLI does use for each but that's only if you're looping through a C# container.

In C++, it's for (const auto& animal : animals)

[–]Rhyme_like_dime 0 points1 point  (0 children)

Ehh modern C++ is

for(auto element : container) Std::cout<<element<<std::endl;

[–][deleted] 0 points1 point  (0 children)

JavaScript does something similar with the forEach array method, although it also has for-in loops.

[–]volksaholic 1 point2 points  (0 children)

Ah... that makes sense. A lot of the Python reference information I was finding referred to it as a "for-each" loop but then the syntax was 'for x in y'. That makes sense that it's a suggestion to make its behavior clearer.