[Python-3000] optimizing [x]range (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Jul 31 03🔞41 CEST 2007


skip at pobox.com wrote:

Given that I find the cascading comparisons clearer I see no reason to optimize the "in range(...)" case.

The sort of thing I have in mind is where I have a sequence that I want to frequently iterate over the indices of, so I do

r = xrange(len(myseq))

so I can write

for i in r: ...

Having done that, if I want to test whether some index j is within the range of indices for this sequence, it seems natural to write

if j in r: ...

Given the context, I think this is a very Obvious Way To Do It, and it's surprising that it isn't as efficient as it looks like it should be.

-- Greg



More information about the Python-3000 mailing list