0% found this document useful (0 votes)
49 views7 pages

Iterators: Well What Is Iteration???

An iterator is an object that can be used to iterate over iterable objects like lists, tuples, and strings. It implements the __iter__() and __next__() methods to return one element at a time. Any object that returns an iterator is considered iterable. The for loop uses iterators under the hood - it first creates an iterator from the iterable, then calls __next__() in a loop until a StopIteration error is raised. Users can also manually iterate with next(iterator). Classes can define their own iterators by implementing these magic methods.

Uploaded by

Prameela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views7 pages

Iterators: Well What Is Iteration???

An iterator is an object that can be used to iterate over iterable objects like lists, tuples, and strings. It implements the __iter__() and __next__() methods to return one element at a time. Any object that returns an iterator is considered iterable. The for loop uses iterators under the hood - it first creates an iterator from the iterable, then calls __next__() in a loop until a StopIteration error is raised. Users can also manually iterate with next(iterator). Classes can define their own iterators by implementing these magic methods.

Uploaded by

Prameela
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Iterators

Well ………what is iteration???


The repetition of a process in order to
generate a sequence
Example: looping
An iterator can iterate

What is an Iterator???
An object which will return data … one
element at a time

How can it return one element at a


time?????????????????

Because it implements the protocol


consisting of __iter__() and __next__()
methods

Who can be iterated upon?????


An iterable can be iterated upon
What is an Iterable???
Any object that returns an iterator to
iterate through its values is said to be
iterable
Example: List,Tuple,String
Example :
To create an iterable and iterate
through it
mylist=[1,2,3,4,5]
I have created an iterable
Now I will create an iterator to iterate
through it….
myiter=iter(mylist)
Now I will use the next() function to
manually iterate through all the
values of the iterable
Print(next(myiter)
or
print(myiter.__next__())
print(myiter.__next__())
print(myiter.__next__())
print(myiter.__next__())
print(myiter.__next__())
Can this be automated???????????????
Yes………
Using a for loop

Lets automatically iterate through an


iterable using a for loop
First create an iterable
mynum = [1,2,3,4,5]
Then iterate through it using a for loop
For n in mynum:
Print (n)
1
2
3
4
5

The for loop first creates an iterator object


and then executes the next() method in an
infinite while loop using Try block so as to
terminate when the end of the loop is
reached

Internal implementation of a FOR loop


Iterator_object=iter(iterable)
While True:
try:
element=next(Iterator_object)
print(element)
except StopIteration:
break

How to iterate through a tuple

mytuple = ("apple", "banana", "cherry") apple


myiter = iter(mytuple) banana
print(next(myiter)) cherry
print(next(myiter))
print(next(myiter))

How to iterate through a string


mystring = "REVA University" R
myit= iter(mystring) E
print(next(mystring)) V
print(next(mystring)) A
print(next(mystring))
print(next(mystring)) U
print(next(mystring))
print(next(mystring))
r

How to create an object/class as an


Iterator?
To create an object/class as an iterator
we have to implement the
methods __iter__() and __next__() to our
object.
We already know that the __init__() allows
us to automatically initialize when the
object is being created.
Likewise the __iter__() method in a class
always returns the iterator object .
And the __next__() method returns the
next item in the sequence.
Example

Create an iterator class that returns


numbers starting with 1 and each
sequence will increase by one
(returning 1,2,3,4,5 etc…)

class MyNumbers: 1
  def __iter__(self): 2
    self.a = 1 3
    return self
4
  def __next__(self): 5
    x = self.a
    self.a += 1
    return x

myclass = MyNumbers()
myiter = iter(myclass)

print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))
print(next(myiter))

You might also like