How to make an iterator in Python
To create an iterator object in Python, use the function iter()
iter(x)
The parameter x is an iterable object ( list, tuple, string, etc. ).
The iter() function creates an iterator object.
Examples
Example 1 ( list )
Create an iterator from a list using the iter() function
>>> x=iter([1,2,3,4,5])
The iterator object can be read via the next() function
>>> next(x)
1
>>> next(x)
2
Example 2 ( tuple )
Create an iterator from a tuple
>>> x=iter((1,2,3,4,5))
Read iterator elements using next() function
>>> next(x)
1
>>> next(x)
2
Example 3 ( string )
Create an iterator from a string
>>> x=iter("how")
Read iterator elements of a string using next() function
>>> next(x)
h
>>> next(x)
o
>>> next(x)
w