tags:

views:

86

answers:

1

I am trying to build a range with an upper bound bigger than limit of integer in Python. I was looking for something like the following:

import sys
start = 100;
step = 100;
limit = sys.maxint + 1;
result = xrange(start, limit, step);

However, xrange parameters are limited to the integer. According to Python Standard Library, I have to use itertools module: islice(count(start, step), (stop-start+step-1)//step), but islice seems to have the same integer limitations for the parameters.

Is there any other way to build the list with upper limit represented by long integer?

+7  A: 

xrange is trivial to roll out on your own, so you can do just that:

def xlongrange(start, limit, step):
    n = start
    while n < limit:
        yield n
        n += step
Pavel Minaev
Thank you. I was looking for some standard function, so overlooked writing my own solution.
artdanil
Doesn't work with negative step. Goes into infinite loop if step == 0 and start < limit.
John Machin
Writing a formal spec, supporting negative step and adding argument checking is left as an exercise for the reader ;)
Pavel Minaev