Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | range() does not return an iterator

  • Difficulty Level : Basic
  • 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.

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

# Python program to understand range
# this creates a list of 0 to 5
# integers
demo = range(6)
# print the demo
# it will generate error


 range(0, 6)

Runtime Errors :

 Traceback (most recent call last):
  File "/home/", line 13, in 
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

# Python program to understand range
# creates an iterator
demo = iter(range(6))
# print iterator
# use next


<listiterator object at 0x7f3f32a46450 >

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


# Python program to understand range
# creates a demo range
demo = range(1, 31, 2)
# print the range
# print the start of range
# print step of range
# print the index of element 23
# since 30 is not present it will give error


range(1, 31, 2)

Runtime Error : Since element 30 is not present it will rise an error

 Traceback (most recent call last):
  File "/home/", line 19, in 
ValueError: 30 is not in range

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!