Is using range() in for loops really Pythonic?

castironpi at gmail.com castironpi at gmail.com
Sun May 11 04:09:21 EDT 2008


On May 11, 12:38 am, Paul Rubin <http://phr...@NOSPAM.invalid> wrote:
> John Salerno <johnj... at NOSPAMgmail.com> writes:
> > for x in range(10):
> >     #do something 10 times
>
> > is unPythonic. The reason I ask is because the structure of the for loop
> > seems to be for iterating through a sequence. It seems somewhat
> > artificial to use the for loop to do something a certain number of
> > times, like above.
>
> It is pretty natural in imperative-style code.  The one thing I'd do
> differently is use xrange instead of range, to avoid creating a
> 10-element list in memory before starting the loop.

If you give extras to memory, can Python live?



More information about the Python-list mailing list