Iterable vs Iterator in Python

Let’s learn about iterables and iterators.

Photo by Jude Beck on Unsplash


In Python, an iterator is an object which implements the iterator protocol, which consist of the methods __iter__() and __next__() .

  • An iterator is an object representing a stream of data.
  • It returns the data one element at a time.
  • A Python iterator must support a method called __next__() that takes no arguments and always returns the next element of the stream.
  • If there are no more elements in the stream,__next__() must raise the StopIteration exception.
  • Iterators don’t have to be finite.It’s perfectly reasonable to write an iterator that produces an infinite stream of data.


In Python,Iterable is anything you can loop over with a for loop.

An object is called an iterable if u can get an iterator out of it.

  1. Calling iter() function on an iterable gives us an iterator.
  2. Calling next() function on iterator gives us the next element.
  3. If the iterator is exhausted(if it has no more elements), calling next() raises StopIteration exception.

Image Source: Author

Topics Covered in this story

Image by Author

How to convert an object into Iterator

Using iter() function.

The built-in iter() function takes an arbitrary object and tries to return an iterator that will return the object’s contents or elements, raising TypeError if the object doesn’t support iteration.

Iterables vs Iterators.

Iterables vs Iterators.
  1. Both iterables and iterators can be iterated using for loop.

2. Iterables supports only iter() function.But iterators supports both iter() and next() function.


Iterable supports only iter() function(Image Source: Author)


Iterator supports iter() and next() function. (Image Source: Author)

3. Iterators are also iterables.

We can get an iterator from an iterable by calling iter() function.Similarly, we can call iter() function on the iterator itself. It will return the iterator object itself.

Data types that support Iterators

data types that support Iterators (Image Source: Author)
  • Text Sequence type- str

Strings support Iterator. iter() on a string returns an iterator.

  • Sequences in Python: List, tuple, range supports Iterator.

1.Converting list(iterable) into iterator object.

2. Converting tuple(iterable) into iterator object.

3.Converting range() into iterator object.

  • Mapping: Dictionary supports Iterator.

iter()function will loop over dictionary keys only.

Dictionary has methods like 
1.values()– which will iterate over values
2.items() -which will iterate over (key, value) pairs.

items()-loop over (key,value) pair in the dictionary.

  • set

File objects are implemented as iterators.

Files also support iteration by calling the readline() method until there are no more lines in the file.
readline()-Read until newline or EOF and return a single str. If the stream is already at EOF, an empty string is returned.



How to convert an iterator to list/tuple/dict

Iterators can be materialized as lists or tuples by using the list() ortuple() constructor functions.The dict() constructor can accept an iterator that returns a finite stream of (key, value) tuples.

Functions which returns an iterator

Functions that return iterators (Image Source: Author)
  1. Generator
    Generators are a special class of functions that return an iterator that returns a stream of values.
    Any function containing a yield keyword is a generator function.

2. Generator expression
Generator expression returns an iterator object only. Refer to my story for generator expressions.

Built-in functions that return an iterator.

  1. map()
    Return an iterator that applies a function to every item of iterable, yielding the results.

Refer to my story of map() vs starmap()

2. filter()
Returns an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container that supports iteration, or an iterator.

Refer to my story of filter vs filterfalse

3. zip()
zip(iter a, iter b ..) —Returns a zip object which is an iterator.
It takes one element from each iterable and returns them in a tuple.

4. enumerate():
enumerate(iterable, start=0)
Return an enumerate object which is an iterator. 
iterable must be a sequence, an iterator, or some other object which supports iteration. 
The __next__() method of the iterator returned by enumerate()returns a tuple containing a count (from the start which defaults to 0) and the values obtained from iterating over iterable.

5.itertools module:

The itertools module contains a number of commonly-used iterators as well as functions for combining several iterators.

Refer to my story for itertools module.


reversed() function is used to reverse the elements in the sequences (list,tuple,range()).It is also used to reverse the key elements in the dictionary.
reversed() function returns an iterator object.

The built-in functions which supports Iterator.

also supports a single iterator argument and returns the largest element.

2. min()
also supports a single iterator argument and returns the smallest element.
If iterator is infinite,max() and min() will never return.It supports only finite iterators.


in, not in

The "in" and "not in" operators also support iterators. X in iterator is true if X is found in the stream returned by the iterator. 
If the iterator is infinite, and if the element X never appears in the stream, the "in" and "not in" operators will never return.

Sequence unpacking

Sequence unpacking also supports iterators.

Sequence unpacking requires the list of variables on the left to have the same number of elements in the iterator.

for loop

In the statement for X in Y, Y must be an iterator or some object(iterable) for which iter() can create an iterator.
 These two statements are equivalent:

for i in iter(obj):
for i in obj:

Limits of Iterator

  • We can only go forward in an iterator.
  • We can’t make a copy of it.
  • No way to get the previous element.
  • We can’t reset the iterator.

  • The iterator protocol only specifies the __next__() method. Functions may therefore consume all of the iterator’s output, and if you need to do something different with the same stream, you’ll have to create a new iterator.


Raised by the built-in function next()and an iterator’s __next__() method to signal that there are no further items produced by the iterator.


  • Iterators can be infinite or finite.
  • max(),min() doesn’t support infinite iterators.
  • len() is not supported by iterators.
  • for loop can be used to iterate over iterable and also iterator.
  • Iterators:
    1. We can iterate using for loop
    2. We can use the next() method which will return the next element in the iterator.
    3. We can convert the iterator to list using list() constructor, tuple using tuple () constructor.
    4.The dict() constructor can accept an iterator that returns a finite stream of (key, value) tuples
  • Difference between iterables and iterators:
    1. Iterators support iter() and next() function. Iterables support only iter() function.
    2. Iterators are also iterables but not vice versa.

Resources(Python Documentation):



Iterator Types



Make a one-time donation

Make a monthly donation

Make a yearly donation

Choose an amount


Or enter a custom amount


Your contribution is appreciated.

Your contribution is appreciated.

Your contribution is appreciated.

Buy Me a CoffeeBuy Me a CoffeeBuy Me a Coffee

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s