Python | range() does not return an iterator
Last Updated :
13 Nov, 2018
range() : Python range function generates a list of numbers which are generally used in many situation for iteration as in for loop or in many other cases. 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. We can call next if we get an iterator using iter.
Python3 1==
# Python program to understand range
# this creates a list of 0 to 5
# integers
demo = range(6)
# print the demo
print(demo)
# it will generate error
print(next(demo))
OUTPUT :
range(0, 6)
Runtime Errors :
Traceback (most recent call last):
File "/home/6881218331a293819d2a4c16029084f9.py", line 13, in
print(next(demo))
TypeError: list object is not an iterator
Note : Above runtime error clearly indicates that python range is not a iterator.
Because
range is iterable so we can get a iterator with the help of them but we can not directly call next in next. Below example explains it clearly
Python3 1==
# Python program to understand range
# creates an iterator
demo = iter(range(6))
# print iterator
print(demo)
# use next
print(next(demo))
OUTPUT :
<listiterator object at 0x7f3f32a46450 >
0
range does not generates all numbers that it contains when we create it. It gives only those numbers which we get them using loop. range has following properties.
- range objects are immutable means that they can not be changed again so they can be used as index in dictionaries.
- They have start stop and step arguments .
- same range can be visit again and again
Example
Python3 1==
# Python program to understand range
# creates a demo range
demo = range(1, 31, 2)
# print the range
print(demo)
# print the start of range
print(demo.start)
# print step of range
print(demo.step)
# print the index of element 23
print(demo.index(23))
# since 30 is not present it will give error
print(demo.index(30))
OUTPUT :
range(1, 31, 2)
1
2
11
Runtime Error : Since element 30 is not present it will rise an error
Traceback (most recent call last):
File "/home/cddaae6552d1d9288d7c5ab503c54642.py", line 19, in
print(demo.index(30))
ValueError: 30 is not in range
Similar Reads
range() to a list in Python In Python, the range() function is used to generate a sequence of numbers. However, it produces a range object, which is an iterable but not a list. If we need to manipulate or access the numbers as a list, we must explicitly convert the range object into a list. For example, given range(1, 5), we m
2 min read
Iterate over a set in Python The goal is to iterate over a set in Python. Since sets are unordered, the order of elements may vary each time you iterate. There are several ways to access and process each element of a set in Python, but the sequence may change with each execution. Let's explore different ways to iterate over a s
2 min read
Python - Loop Through a Range Looping through a range is an important operation in Python. In this article, we will explore the different ways to loop through a range in Python, demonstrating how we customize start, end, and step values, as well as alternative methods for more advanced use cases like looping through floating-poi
3 min read
Python - itertools.repeat() Pythonâs Itertool is a module that provides various functions that work on iterators to produce complex iterators. This module works as a fast, memory-efficient tool that is used either by themselves or in combination to form iterator algebra. Note: For more information, refer to Python Itertools re
2 min read
Iterate over a tuple in Python Python provides several ways to iterate over tuples. The simplest and the most common way to iterate over a tuple is to use a for loop. Below is an example on how to iterate over a tuple using a for loop.Pythont = ('red', 'green', 'blue', 'yellow') # iterates over each element of the tuple 't' # and
2 min read