Python Iterator

Rumman Ansari   Software Engineer   2022-10-03   219 Share
☰ Table of Contents

Table of Content:


  • An Iterator is an object, which allows a programmer to traverse through all the elements of a collection, regardless of its specific implementation.

  • Values of an Iterator can be accessed only once and in sequential order.

Sample Iterator

x = [6, 3, 1]
s = iter(x)            
print(next(s))      # -> 6
print(next(s))      # -> 3
print(next(s))      # -> 1
print(next(s))      # -> StopIteration Error