I want to run a range from a start to an end value. It works fine on low numbers but when it gets too large it causes an overflow error as int too large to convert to C Long. I am using Python 2.7.3.
I read here OverflowError Python int too large to convert to C long on using the itertools.count()
method except that method works differently to the xrange
method by stepping as opposed to declaring an end range value.
Can the itertools.count()
be set up to work like xrange()
?
print "Range start value"
start_value = raw_input('> ')
start_value = int(start_value)
print "Range end value"
end_value = raw_input('> ')
end_value = int(end_value)
for i in xrange(start_value, end_value):
print hex(i)
You can use
itertools.islice()
to givecount
an end:islice()
raisesStopIteration
afterend_value - start_value
values have been iterated over.Supporting a step size other than 1 and putting it all together in a function would be:
then use
irange()
like you'd userange()
orxrange()
, except you can now use Pythonlong
integers: