Tuesday, October 10, 2023

Iterators in Python (Theory)

  • An iterator object is used to iterate through all items/values of a Iterable Object
  • To iterate we need to get Iterator object from Iterable Object
  • iter() and next() method are associated with Iterator object
  • String, Tuple, List, Set and Dictionaries all are iterable objects
  • There is built in iter() method, that is used to get Iterator object from an iterable object
  • We can iterate all items of an iterable object using for loop as well
  • For loop automatically create an Iterator object for an iterable object and call next() method for iterating through all items
  • To create an object/class as an iterator you have to implement the methods __iter__() and __next__() to your object

No comments:

Post a Comment