Is using range() in for loops really Pythonic?

Daniel Fetchinson fetchinson at googlemail.com
Sat May 10 22:35:39 EDT 2008


I know it's popular and very handy, but I'm curious if there are purists
> out there who think that using something like:
>
> 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.
>
> Anyone out there refuse to use it this way, or is it just impossible to
> avoid?



IMHO it's perfectly okay to use it as you describe it. Maybe you can switch
to xrange( ) which doesn't return a list but an iterator. Note that in
python 3.0 xrange will cease to exist and range( ) will return an iterator
instead.

Cheers,
Daniel

-- 
http://www.cafepress.com/putitdown
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20080510/6f1952af/attachment-0001.html>


More information about the Python-list mailing list