I was going through Python Generators Wiki when I came across this RangeGenerator page which talks about irange()
-
This will let us iterator over large spans of numbers without resorting to xrange, which is a lazy list as opposed to a generator.
I can't seem to understand the test suite and the implementation described on that page. I know that range()
creates a list in the memory (from Python 2.7 point of view) and xrange()
is a generator. How is irange()
any different?
range() – This returns a list of numbers created using range() function. xrange() – This function returns the generator object that can be used to display numbers only by looping. Only particular range is displayed on demand and hence called lazy evaluation .
The syntax of xrange is the same as range() which means in xrange also we have to specify start, stop and step. If you are using Python 3 and execute a program using xrange then it will generate an error since this version onwards range() is used.
The xrange() function in Python is used to generate a sequence of numbers, similar to the range() function. However, xrange() is used only in Python 2. x whereas range() is used in Python 3. x.
Using the enumerate() Function.
irange()
returns a generator type, which can only be iterated over. Nothing else. Once you iterated over it, the generator is exhausted and can not be iterated over again.
The Python 2 xrange()
type and Python 3 range()
type are sequence types, they support various operations that other sequences support as well, such as reporting on their length, test for containment, and indexing:
>>> xr = xrange(10, 20, 3)
>>> len(xr)
4
>>> 10 in xr
True
>>> xr[0]
10
>>> xr[1]
13
You can iterate over these objects more than once:
>>> for i in xr:
... print i,
...
10 13 16 19
>>> for i in xr:
... print i,
...
10 13 16 19
You can even use the reversed()
function to iterate over them in reverse, efficiently:
>>> for i in reversed(xr):
... print i,
...
19 16 13 10
The Python 3 range()
type is an improved version of xrange()
, in that it supports more sequence operations, is more efficient still, and can handle values beyond sys.maxint
(what would be a long
integer in Python 2).
It supports slicing, for example, which results in a new range()
object for the sliced values:
>>> r = range(10, 20, 3)
>>> r[:2]
range(10, 16, 3)
You can use negative indices just like you can with other Python sequences, to get elements counting from the end:
>>> r[-2]
16
>>> r[-2:]
range(16, 22, 3)
and the type supports testing for equality; two range()
instances are equal if they'd yield the same values:
>>> range(10, 20, 3) == range(10, 21, 3)
True
In Python 2, the only advantage the generator irange()
might have is that it doesn't suffer from the limitation to non-long integers that xrange()
is subjected to:
>>> import sys
>>> xrange(sys.maxint)
xrange(9223372036854775807)
>>> xrange(sys.maxint + 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OverflowError: Python int too large to convert to C long
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With