Enable Dark Mode!
iterator-and-generator-features-in-python.jpg
By: Anagha NK

Iterator and Generator Features in Python

Technical

Iterator

An iterator is an object which contains a countable number of values. The iterator can traverse through all values, meaning it's an object it can iterate upon. Technically it can be defined as an iterator, an object used to implement the iterator protocols. Also, an iterator can be defined as a repeating process, which is repeated many times using the same logic as applied.

The primary purpose of the iterator is to allow every element of the container to use when it is isolated from the internal structure of the container. This way enables the container which stores the details in any manner. So, in this case, it treats the user as a simple sequence or a list. Iteration can be achieved by using a loop like for, etc.

There are two methods used here.

__iter__() 

__next__()

Iterator uses the __next__() method when iterating over the iterable object.

In __next__() method returns the next item of the object. Note that every iterable is not an iterator. A good example is taken, for it is considered a list.

Using iter() can create an iterator from iterable. Making it possible requires an object for class and either a method __iter__. It returns an iterator or a _getitem_ using a method with sequential indexes that starts with 0.

For Example,

iterator_test = iter([10, 20, 30, 40, 50])

print(type(iterator_test))

print(next(iterator_test))

print(next(iterator_test))

print(next(iterator_test))

print(next(iterator_test))

print(next(iterator_test))

# Once the iterator is exhausted, next() function raise StopIteration.

print(next(iterator_test))

Output:

<class 'list_iterator'>

10

20

30

40

50

StopIteration - - -

In the above example first created an iterator named iterator_test. Then pass a list of numbers using the method iter(). After that, use the type() method to check the type. So here found an indeed iterator more than a list_iterator(). The next() method produces an integer on each iteration and stops until an exception occurs.

Iterable

Iterable is also an object which can iterate over. It's generated by using the __iter__() method. It's inherited from iterations.

It is to be looped over or iterated over with the help of a for loop used.

Examples of iterable are the objects like lists, tuples, sets, dictionaries, strings, etc., 

Iterable also says anything that you can loop over.


For Example,

1. # String as an iterable

for i in PYTHON:

     print(i)


2. # list as an iterable

for i in [1, 2, 3,4]:

     print(str(i*2))


Output

1. P

Y

T

H

O

N


2. 2

4

6

8

Generators

A Python generator is a function that lends us a sequence of values to iterate on it. This function is used to implement a generator. It used the yield keyword, and all local variables were stored before the yield. Generators are mainly used in loops to generate iterators. It returns all the values in a loop without affecting the iterating loop. Every generator is an iterator, but every iterator is not a generator.

Example:

def square(n):
    for i in range(1, n+1):
        yield i*i
a = sq_numbers(3)
print("The square of given numbers are : ")
print(next(a))
print(next(a))
print(next(a))

Output:

The square of number 1,2,3 are :  

1

4

9

This way we can use iterator and generator in python. The generator is a more efficient memory system to generate sequence types And iterators collect objects through the loop.



If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp_icon
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message