Python Iterator

What is an iteration?

An iteration is a process of iterating through the loop. Basically, the python iterator is a type object that can be used with a for in loop. Python list, dict, tuples, and sets are the examples of the python iterators. Let us get more idea on an iteration with an example

Ex: a=[‘python ‘,’online’,’training’]
for i in a:

Code explanation:

In the above given example, a is an array consists of a series of elements. Now our aim is to print all the elements in the elements. This can be done with the variable i. Here the variable I is called the python iterator. Here we called the variable I as an iterator since it was iterated through an array [].
In simple words, an iterator is an element, that performs the iteration process.
But in fact, if any object wants to be an iterator, it must implement the following methods :


At the time of initialization, an _iter_ method is called. Besides this _iter_ method should return the object that contains the next (or) _next_ method.


The next method should return the next variable value. On the iterator object, when an iterator is used within a ‘’for in”, the loop implicitly calls the next(). Besides this method should raise the stop iteration (). This indicates the end of the iteration.
The syntax of the iterator object is shown below :
while (condition)
# To get the next item
element = next(iter_obj)
# perform some op
except StopIteration:
# At this point the loop terminates

Ex :
class test:
def __iter__(self):
self.temp = 1
return self
def __next__(self):
if self.temp <= 5:
x = self.temp
self.temp+= 1
return x
raise StopIteration
var = test()
myiter = iter(var)
for x in myiter:
OutPut :
Note :
There is no restriction of the iterators to be finite. i.e there can be situation ,where an iterator to be infinite. So the people must be careful while using iterators.
The advantage of using iterators is that we can save resources like memory.
As explained above, in iterators, we need to perform the two functions like iter() and next(). So implementing these two functions is lengthy. So we need an alternative to this concept.

To get in-depth knowledge on Python, you can enroll for live python online training by OnlineITGuru with 24/7 support and lifetime access

to our newsletter

Drop Us A Query

Trending Courses
  • Selenium with python
    Selenium with Python Training
  • As we know, that Selenium with Python Web Browser Selenium Automation is Gaining Popularity Day by Day. So many Frameworks and Tools Have arisen to get Services to Developers.

  • Deep learning course
    Deep Learning Course
  • Artificial Intelligence, Deep mastering (DL) is completely about, many levels of Representation and sort of abstraction. That guide to design a sense of Information like Images, sound and text format.

  • machine learning with python
    Machine Learning with Python Training
  • Over last few years, Big Data and analysis have come up, with Exponential and modified Direction of Business. That operate Python, emerged with a fast and strong Contender for going with Predictive Analysis.

  • Data science with R
    Data Science With R Training
  • Understanding and using Linear, non-linear regression Models and Classifying techniques for stats analysis. Hypothesis testing sample methods, to get business decisions.

  • data science with python
    Data Science with Python Training
  • Everyone starts Somewhere, first you learn basics of Every Scripting concept. Here you need complete Introduction to Data Science python libraries Concepts.

  • devops with azure
    Devops with Azure Training
  • As we Know Azure DevOps is a Bunch of Services, in guiding Developers. It contains CI/CD, pipelines, code Repositories, Visual Reporting Tools and more code management with version control.

100% Secure Payments. All major credit & debit cards accepted.