[Tutor] replacing range with xrange

Kent Johnson kent37 at tds.net
Mon Jan 28 15:13:27 CET 2008


Andy Cheesman wrote:

> After watching a nice Google video on Python 3K, and seeing the 
> forthcoming removal of range, I've looked at substitution range with 
> xrange within my code. Direct substitution works for 90% percent of the 
> case (i.e.   for thing in xrange(number):  ), however i can't change the 
> example below where I need a non-continuous range. Any suggestions?
> 
> Andy
> 
> x = range(10)  + range(20, 30)

x = list(range(10)) + list(range(20, 30))

or

from itertools import chain
for thing in chain(range(10), range(20, 30)):

which avoids creating the intermediate lists.

Kent


More information about the Tutor mailing list