What are the most basic definitions of "iterable", "iterator" and "iteration in Python?
I've read multiple definitions but their exact meaning still won't sink in.
Can someone please help me with the basic idea?
What are the most basic definitions of "iterable", "iterator" and "iteration in Python?
I've read multiple definitions but their exact meaning still won't sink in.
Can someone please help me with the basic idea?
Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.
In Python, iterable and iterator have specific meanings.
An iterable is an object that has an
__iter__
method which returns an iterator, or which defines a__getitem__
method that can take sequential indexes starting from zero (and raises anIndexError
when the indexes are no longer valid). So an iterable is an object that you can get an iterator from.An iterator is an object with a
next
(Python 2) or__next__
(Python 3) method.Whenever you use a
for
loop, ormap
, or a list comprehension, etc. in Python, thenext
method is called automatically to get each item from the iterator, thus going through the process of iteration.A good place to start learning would be the iterators section of the tutorial and the iterator types section of the standard types page. After you understand the basics, try the iterators section of the Functional Programming HOWTO.
Here's the explanation I use in teaching Python classes:
An ITERABLE is:
for x in iterable: ...
oriter()
that will return an ITERATOR:iter(obj)
or__iter__
that returns a fresh ITERATOR, or it may have a__getitem__
method suitable for indexed lookup.An ITERATOR is an object:
__next__
method that:StopIteration
__iter__
method that returnsself
).Notes:
__next__
method in Python 3 is speltnext
in Python 2, andnext()
calls that method on the object passed to it.For example:
Iterable:- something that is iterable is iterable; like sequences like lists ,strings etc. Also it has either the
__getItem__()
method or aniter()
function which returns an iterator.Iterator:- When we get iterator object from the
iter()
method of iterable; we call__next__()
method (in python3) or simplynext()
(in python2) to get elements one by one. This class or instance of this class is called an iterator.From docs:-
The use of iterators pervades and unifies Python. Behind the scenes, the for statement calls
iter()
on the container object. The function returns an iterator object that defines the method__next__()
which accesses elements in the container one at a time. When there are no more elements,__next__()
raises a StopIteration exception which tells the for loop to terminate. You can call the__next__()
method using thenext()
built-in function; this example shows how it all works:Ex of a class:-
so,
iterable
is an object that can be looped over. e.g. list , string , tuple etc.using the
iter
function on ouriterable
object will return an iterator object.now this iterator object has method named
__next__
(in Python 3, or justnext
in Python 2) by which you can access each element of iterable.so, OUTPUT OF ABOVE CODE WILL BE:
1
2
Luciano Ramalho, Fluent Python.
Before dealing with the iterables and iterator the major factor that decide the iterable and iterator is sequence
Sequence:Sequence is the collection of data
Iterable:Iterable are the sequence type object that support Iter method.
Iter method:Iter method take sequence as an input and create an object which is known as iterator
Iterator:Iterator are the object which call next method and transverse through the sequence.On calling the next method it returns the object that it transversed currently.
example:
x is a sequence which consists of collection of data
On calling iter(x) it returns a iterator only when the x object has iter method otherwise it raise an exception.If it returns iterator then y is assign like this:
As y is a iterator hence it support next() method
On calling next method it returns the individual elements of the list one by one.
After returning the last element of the sequence if we again call the next method it raise an StopIteration error
example: