Is using range() in for loops really Pythonic?

John Salerno johnjsal at gmailNOSPAM.com
Sun May 11 16:16:53 EDT 2008


XLiIV wrote:

> The range() function returns a list and list is a sequence, isn't?

I think you're missing the point. To me, there seems to be a fundamental 
difference between these two things:

---

people = ['Sam', 'Bob', 'Fred']

for name in people:
     print name

---

AND

---

num = 33

for x in xrange(10):
     print num += 1

---

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.



More information about the Python-list mailing list