Search code examples
pythonrangenumbersxrange

range and xrange for 13-digit numbers in Python?


range() and xrange() work for 10-digit-numbers. But how about 13-digit-numbers? I didn't find anything in the forum.


Solution

  • You could try this. Same semantics as range:

    import operator
    def lrange(num1, num2 = None, step = 1):
        op = operator.__lt__
    
        if num2 is None:
            num1, num2 = 0, num1
        if num2 < num1:
            if step > 0:
                num1 = num2
            op = operator.__gt__
        elif step < 0:
            num1 = num2
    
        while op(num1, num2):
            yield num1
            num1 += step
    
    >>> list(lrange(138264128374162347812634134, 138264128374162347812634140))
    [138264128374162347812634134L, 138264128374162347812634135L, 138264128374162347812634136L, 138264128374162347812634137L, 138264128374162347812634138L, 138264128374162347812634139L]
    

    Another solution would be using itertools.islice, as suggested inxrange's documentation