Why I think range is a wart.

John Roth johnroth at ameritech.net
Wed Mar 13 11:48:12 EST 2002


"Gonçalo Rodrigues" <op73418 at mail.telepac.pt> wrote in message
news:ltsu8us0qdsj6r8oersrac465589ot644c at 4ax.com...
> Some tens (or hundreds - I lost count already) of posts ago, someone
> asked what was the wartiness of the range (and by extension, of
xrange)
> builtin. The question sunked in my brain, and last night it resurfaced
> back with an accompanying answer. Actually, the title is just a bait:
it
> should be named, Why I think the use I make of range is a wart.
>
> For starters, range (and xrange) is a perfectly sensible, reasonable
and
> useful buit-in. But the biggest use I make of it is in iterating
through
> a list, e.g. as in
>
> for index in range(len(mylist)):
>     <whatever>
>
> and in the <whatever> body I need to make use of the indexes. I am not
> sufficiently qualified to say if this is Pythonic or not, but I find
> myself doing this quite often.

It happens to me too, mostly when I'm using a sliding window
type algorithm, that is, I need to look at two adjacent members
of the list. You can't do this easily by iterating through the list;
unrolling the first entry and the last entry are a pain.

Is there a way of handling this nicely? Somehow I doubt it.

John Roth

> Gonçalo Rodrigues
>
> P.S: PEP 279 proposes adding indexed to generators which would "clean"
> the above wart.
>





More information about the Python-list mailing list