Python Iterators Vs Iterables Vs Generators

Anubha Singh
Analytics Vidhya
Published in
2 min readNov 8, 2020

This Triplet is one of the most confusing things in Python Programming and one of the most asked questions in interviews. Let’s clear this confusion one by one in detail:-

Iterators

An iterator is an object that contains a countable number of values.
Technically, in Python, an iterator is an object which implements the iterator protocol, which consists of the methods __iter__() and __next__().


iter_obj=iter([3,4,5])
This creates an iterator.

next(iter_obj)

This return the first element i.e 3

next(iter_obj)

This returns the next element i.e 4 and so on.

An example of how to create your own iterator:-

Let’s create an iterator which counts till 10.

class MyNumbers:
def __iter__(self):
self.a = 1
return self
def __next__(self):
if self.a <= 10:
x = self.a
self.a += 1
return x
else:
raise StopIteration
myclass = MyNumbers()
myiter = iter(myclass)
for x in myiter:
print(x)

Iterables

Lists, tuples, dictionaries, and sets are all iterable objects. They are iterable containers which you can get an iterator from. All these objects have an iter() method which is used to get an iterator.
Example:-

mytuple = (“red”, “blue”, “green”)
myit = iter(mytuple)
print(next(myit))

This will return red.

print(next(myit))

This will return blue.

print(next(myit))

This will return green.

Here mytuple is iterable and myit is iterator.

Now let’s meet our final guest:-

Generators
A python generator function lends us a sequence of values to python iterate on.
Let’s implement a generator:-

def even(x):
while(x!=0):
if x%2==0:
yield x
x-=1
for i in even(8):
print(i)

The output is:-
8
6
4
2

You must have seen some of the notable difference between a generator and iterator from the above example.Let’s Discuss them:-

  1. We used a function to create generator. But in creating an iterator in python, we used the iter() and next() functions.
  2. A generator in python makes use of the ‘yield’ keyword. A python iterator doesn’t.
  3. Python generator saves the states of the local variables every time ‘yield’ pauses the loop in python. An iterator does not make use of local variables, all it needs is iterable to iterate on.
  4. A generator may have any number of ‘yield’ statements.
  5. You can implement your own iterator using a python class; a generator does not need a class in python.
  6. To write a python generator, you can either use a Python Function or a comprehension. But for an iterator, you must use the iter() and next() functions.

Now Let’s discuss the advantages and disadvantages of generators and iterators:-

  1. Generator is efficient for writing fast and compact code. This is an advantage over Python iterators. They are also simpler to code than do custom iterator.
  2. Python iterator is more memory-efficient.

def func():
i=1while i>0:yield ii-=1
for i in func():
print(i)
func().__sizeof__()

This returns 32 for generators.

But For iterators like:

iter([1,2]).__sizeof__()

We get 16.

Thanks for Reading.If you learned anything from blog please 👏.

--

--