Python Iterator

Click to rate this post!
[Total: 0 Average: 0]

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:
print(i)

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 :

Iter:

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.

Next:

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)
try:
# 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
else:
raise StopIteration
var = test()
myiter = iter(var)
for x in myiter:
print(x)
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

Subscribe
to our newsletter

Drop Us A Query

Trending Courses
  • Microsoft Azure Architect Technologies(AZ-300) | ITGuru
    Microsoft Azure Architect Technologies(AZ-300)
  • Azure is a great Microsoft Cloud Computing platform in providing various cloud services through online. ITGuru Certified Azure Architect certification course gives you the practical knowledge on Azure Cloud platform through real-world use cases from live experts

  • ServiceNow Admin Training | ITGuru
    ServiceNow Admin Training
  • Getting knowledge of cloud platforms like ServiceNow is essential in today’s world for the smooth running of projects in cloud platform. Turn your dream to the reality of becoming the Certified ServiceNow Administrator through ServiceNow Administration online certification Course with practical examples by live industry experts through online at ITGuru with real-world use cases.

  • Workday Financials Training | ITGuru
    Workday Financials Training
  • knowing the basics on any platform like Workday is not enough to sustain the IT industry. Hence it is essential to go beyond on Workday basics like Workday Financials training which lets you know the application of Financials management in real -world use cases from ITGuru Live Experts in a practical way.

  • Workday Training
  • An organization is considered as the best one when it offers the best benefits to the employee. Moreover, the greater the employee benefits, the greater the contribution to the organization. ITGuru let you know the practical workday Human Resource Management(HRM) features with live examples by experts

  • Python Online Course | ITGuru
    Python Online Course
  • Turn your dream into reality by ITGuru live experts with real-world use cases through practical knowledge on python online course and become the certified associate in python programming and become a master in python programming

  • Python Programming | ITGuru
    Python Programming
  • Python is the trending programming language in the IT industry. Mastering in python programming gives you more value among the people in the IT industry. Hence start today to learn python programming online by live experts with real-time uses cases at ITGuru


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

Call Now Button