Iterator And Generator In Python

Introduction

In this article, we will take a walkabout how to create and work with iterators. After the walk-through of iterator, we will also explore how to use generators to efficiently use iterators. Then, we will also take a look at the use cases of iterator and generators.

Iterators

In python, there are multiple data structures available like string, list, dictionary, etc. Let's take an example of string data structure.

> for a in [1, 2, 3, 4]:
     print(a)

If you want to iterate over the list of elements as above then you have to write python code using for loop and display it to the console.

But, python has a built-in iterator protocol that takes iterator object as an input and returns an iterator. List, String, Dictionary, tuple is an example of an iterator object.

Iteration protocol has built-in function names iter().

>>> a = iter([11,22])
>>> a
<listiterator object at 0x1004ca850>
#After first iteration
>>> next(x)
11
#After second iteration
>>> next(x)
22
>>> next(x)
  File "<stdin>", line 1, in <module>
StopIteration

While using iterator, we use the iter() method to iterate over the object. It is used in combination with the next() function which will iterate over the next element of the iterator. The return value of the iterator also returns an iterator object.

Generator

The generator is an abstraction created over Iterator. It simplifies the creation of an iterator. While working with iterator and generator to loop through any data structure, it generates a sequence of values instead of a single value. Generators are also known as 'Lazy Evaluators'.

Example

Here, we are going to work with large files which are inefficient if we use normal python code. But, it is efficient to use a generator in such a scenario as it doesn't load all data in memory.

Code Block to read the file without the generator,

csv_gen = csv_reader("bfs_csv.txt")
for line in csv_gen:
    print(line)

In this case, we can use the generator to avoid having issues with memory overkill during the execution of the python program.

def read(filename):
    for line in open(filename, "r"):
        yield line

yield will result in a generator object.

Generator functions are similar to regular functions but it uses Python yield keyword instead of return.

Yield Function

  • The yield function is a simple in-built function in python like any other python function. It is mainly used to control the flow of the generators similar to the return statement in normal python function.
  • In addition to yield, python also has few advanced generator methods as mentioned below,
    • .send()
    • .throw()
    • .close()

Conclusion

In this article, we explored the iterators and generators in python. We also looked at the actual example on how to use iterators and generators with next and yield keywords. Additionally, we have also explored real-time use cases to use generators in python.


Similar Articles