The Repetition of A Process in Order To Generate A Sequence Example

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Iterators

Well ………what is iteration???

The repetition of a process in order to generate a sequence


Example: looping

An iterator can iterate to produce iterations

What is an Iterator???

An object which will return data … one element at a time in a sequence

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/pointer 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 object to iterate through the iterable


mylist….function as an argument, it returns an iterator object

Whenever I pass the iterable to the iter()

myiter=iter(mylist)

Now I will use the next() function to manually iterate through all the
values of the iterable

Print(next(myiter) 1

Print(next(myiter) 2
Print(next(myiter) 3

Print(next(myiter) 4

Print(next(myiter) 5

Print(next(myiter) error

or

print(myiter.__next__()) 1

print(myiter.__next__()) 2

print(myiter.__next__()) 3

print(myiter.__next__()) 4

print(myiter.__next__()) 5

print(myiter.__next__()) error

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")
myiter = iter(mytuple) print(next(myiter)) apple
print(next(myiter)) banana
print(next(myiter)) cherry
print(next(myiter)) error

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)) n
print(next(mystring))

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
    return self 3
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