<div dir="auto">Iterating over range(len(collection)) is one of the worst anti-patterns in Python. I take great pains to slap my students who do that.</div><div class="gmail_extra"><br><div class="gmail_quote">On Feb 17, 2017 9:32 AM, "Mikhail V" <<a href="mailto:mikhailwas@gmail.com">mikhailwas@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On 17 February 2017 at 17:37, Chris Angelico <span dir="ltr"><<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">On Sat, Feb 18, 2017 at 3:30 AM, Mikhail V <<a href="mailto:mikhailwas@gmail.com" target="_blank">mikhailwas@gmail.com</a>> wrote:<br>
> On 17 February 2017 at 04:59, Chris Angelico <<a href="mailto:rosuav@gmail.com" target="_blank">rosuav@gmail.com</a>> wrote:<br>
>><br>
>> On Fri, Feb 17, 2017 at 2:13 PM, Mikhail V <<a href="mailto:mikhailwas@gmail.com" target="_blank">mikhailwas@gmail.com</a>> wrote:<br>
>> > Common use case:<br>
>> ><br>
>> > L = [1,3,5,7]<br>
>> ><br>
>> > for i over len(L):<br>
>> > e = L[i]<br>
>> ><br>
>> > or:<br>
>> ><br>
>> > length = len(L)<br>
>> > for i over length:<br>
>> > e = L[i]<br>
>><br>
>> Better use case:<br>
>><br>
>> for i, e in enumerate(L):<br>
>><br>
><br>
> This would be more compact, yet less readable, more error prone variant.<br>
> I'd avoid it it all costs and even if I don't need the index further in loop<br>
> body,<br>
> (which happens rarely in my experience) I write e=L[i] in second line<br>
> to make the code more verbose and keep the flow order.<br>
> So your variant (and those proposed in PEP-212) could serve in list<br>
> comprehensions for example, but for common 'expanded' code I find it<br>
> decline of readability, and creating totally different variants for same<br>
> iteration idea.<br>
> But partially that could be simply matter of habit and love to contractions.<br>
<br>
If you don't need the index, why not just iterate over the list directly?<br>
<br>
for e in L:<br>
<br>
That's the single most obvious way to step through a collection in<br>
Python. What do you need to count up to the length for?<br>
<br>
</blockquote></div><br></div><div class="gmail_extra">I have said I need the index, probably you've misread my last comment.<br>Further more I explained why I think iteration over index should be the <br>preferred way, it help with readability a lot.<br></div><div class="gmail_extra">All my learning years ended up with rewriting most code to "for i in range()"<br></div><div class="gmail_extra">and I slap myself when I start to write "for e in L". <br></div><div class="gmail_extra">It is exactly where TOOWTDI applies perfectly and it is integer iteration for me.<br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Mikhail<br></div></div>
<br>______________________________<wbr>_________________<br>
Python-ideas mailing list<br>
<a href="mailto:Python-ideas@python.org">Python-ideas@python.org</a><br>
<a href="https://mail.python.org/mailman/listinfo/python-ideas" rel="noreferrer" target="_blank">https://mail.python.org/<wbr>mailman/listinfo/python-ideas</a><br>
Code of Conduct: <a href="http://python.org/psf/codeofconduct/" rel="noreferrer" target="_blank">http://python.org/psf/<wbr>codeofconduct/</a><br></blockquote></div></div>