Let’s learn about iterables and iterators.
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.
iter()function on an iterable gives us an iterator.
next()function on iterator gives us the next element.
- If the iterator is exhausted(if it has no more elements), calling next() raises
Topics Covered in this story
How to convert an object into Iterator
Using iter() function.
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.
- Both iterables and iterators can be iterated using for loop.
2. Iterables supports only iter() function.But iterators supports both iter() and next() function.
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
- 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
.values()– which will iterate over values
items() -which will iterate over (key, value) pairs.
items()-loop over (key,value) pair in the dictionary.
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.
red green blue
How to convert an iterator to list/tuple/dict
Iterators can be materialized as lists or tuples by using the
tuple() constructor functions.The
dict() constructor can accept an iterator that returns a finite stream of
(key, value) tuples.
Functions which returns an iterator
Generators are a special class of functions that return an iterator that returns a stream of values.
Any function containing a
yieldkeyword 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.
Return an iterator that applies a function to every item of iterable, yielding the results.
Refer to my story of map() vs starmap()
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
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.
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.
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.
max() also supports a single iterator argument and returns the largest element.
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
"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
"not in" operators will never return.
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.
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): print(i)
for i in obj: print(i)
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.
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.
dict()constructor can accept an iterator that returns a finite stream of
- 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.