for i in range() anti-pattern [was Re: trouble writing results to files]

Roberto Bonvallet Roberto.Bonvallet at cern.ch
Thu Nov 30 04:52:35 EST 2006


Steven D'Aprano wrote:
> On Wed, 29 Nov 2006 17:00:30 +0100, Fredrik Lundh wrote:
> 
>> Neil Cerutti wrote:
>> 
>>>> BTW, iterating over range(len(a)) is an anti-pattern in Python.
>>> 
>>> Unless you're modifying elements of a, surely?
>> 
>> and needs to run on a Python version that doesn't support enumerate.
> 
> This isn't meant as an argument against using enumerate in the common
> case, but there are circumstances where iterating with an index variable
> is the right thing to do. "Anti-pattern" tends to imply that it is always
> wrong.

Right, I should have said:  "iterating over range(len(a)) just to obtain the
elements of a is not the pythonic way to do it".

Cheers,
-- 
Roberto Bonvallet



More information about the Python-list mailing list