Trending technologies

#AWS #Azure #Blockchain #IOT #machine learning #Virtual Reality #Analytics #Business Intelligence

Tutorials

Blogs

Iterator is an object which allows a programmer to traverse through all the elements of a collection.

 

in Python, an iterator is an object which implements the iterator protocol, which consist of  the methods __iter__() and __next__().

Iterators have several advantages:

  1. Cleaner code.
  2. Iterators save resources.
  3. Iterators can work with infinite sequences.

Iterators Example

Strings is also iterable objects, it have a sequence of characters.


mystr = "India"

myit = iter(mystr)

print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))
print(next(myit))

Output

I
n
d
i
a

Iterators

Iterable Vs Iterator

Iterators in Python

error: Content is protected !!