Perhaps I've fallen victim to misinformation on the web, but I think it's more likely just that I've misunderstood something. Based on what I've learned so far, range() is a generator, and generators can be used as iterators. However, this code:
myrange = range(10) print(next(myrange))
gives me this error:
TypeError: 'range' object is not an iterator
What am I missing here? I was expecting this to print 0, and to advance to the next value in myrange
. I'm new to Python, so please accept my apologies for the rather basic question, but I couldn't find a good explanation anywhere else.
Unless your generator is infinite, you can iterate through it one time only. Once all values have been evaluated, iteration will stop and the for loop will exit. If you used next() , then instead you'll get an explicit StopIteration exception.
To convert a Python Range to Python List, use list() constructor, with range object passed as argument. list() constructor returns a list generated using the range. Or you can use a for loop to append each item of range to list.
Python | range() does not return an iterator In python range objects are not iterators. range is a class of a list of immutable objects. The iteration behavior of range is similar to iteration behavior of list in list and range we can not directly call next function.
range returns an iterable, not an iterator. It can make iterators when iteration is necessary.
range
is a class of immutable iterable objects. Their iteration behavior can be compared to list
s: you can't call next
directly on them; you have to get an iterator by using iter
.
So no, range
is not a generator.
You may be thinking, "why didn't they make it directly iterable"? Well, range
s have some useful properties that wouldn't be possible that way:
start
, stop
and step
attributes (since Python 3.3), count
and index
methods and they support in
, len
and __getitem__
operations.range
multiple times.>>> myrange = range(1, 21, 2) >>> myrange.start 1 >>> myrange.step 2 >>> myrange.index(17) 8 >>> myrange.index(18) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: 18 is not in range >>> it = iter(myrange) >>> it <range_iterator object at 0x7f504a9be960> >>> next(it) 1 >>> next(it) 3 >>> next(it) 5
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