Is using range() in for loops really Pythonic?
Yves Dorfsman
yves at zioup.com
Sun May 11 18:51:09 EDT 2008
John Salerno wrote:
>
> To me, the first example is a pure use of the for loop. You are
> iterating through an object and *using* the items you are stepping through.
>
> The second example, however, is simply doing something 10 times, and
> what it's doing has nothing to do with 'x' or xrange. So it seems like
> an abuse of the for loop.
Well, I would say this:
myl = ['a', 'b', 'c', 'd']
for i in xrange(len(myl)):
print myl[i]
As you would see in other languages, is an abuse in python. But in you need
to iterate 5 times over something. Do you have a cleaner / python'er
alternative ?
Do you find the following cleaner:
x = 0
while x <= 4:
print x
x += 1
Yves.
http://www.SollerS.ca
More information about the Python-list
mailing list