Change language

Python | range () does not return an iterator

# Python program to understand the range
# this creates a list from 0 to 5
# integers

 

demo = range ( 6 )

 
# print demo

print (demo)

 
# will generate an 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: the above runtime error clearly indicates that the Python range is not an iterator.

Since the range is iterative so we can get an iterator with it, but we cannot directly call next in the following. The example below explains it clearly

# Python range comprehension program

 
# creates an iterator

demo = iter ( range ( 6 ))

  
# print the iterator

print (demo)

 
# use the following

print ( next (demo))

OUTPUT:

 "listiterator object at 0x7f3f32a46450" 0 

The range does not generate all the numbers it contains when we create it. It only gives the numbers that we get using the loop. Range has the following properties.

  • Range objects are immutable, which means they cannot be changed again, so they can be used as an index in dictionaries.
  • They have there are start and end arguments.
  • the same range can be visited over and over

example

# Python range comprehension program

 
# creates a demo range

demo = range ( 1 , 31 , 2 )

  # print range

print (demo)

 
# print start of range

print (demo.start)

 
# range printing step

print (demo. step)

 
# display the index of element 23

print (demo.index ( 23 ))

 
# since 30 no this will give an error

print (demo.index ( 30 ))

OUTPUT:

 range (1, 31, 2) 1 2 11 

Runtime error: because element 30 is missing, an error occurs

 Traceback (most recent call last): File "/home/cddaae6552d1d9288d7c5ab503c54642.py", line 19, in print (demo.index (30)) ValueError: 30 is not in range 

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically