0 votes
1 view
in Python by (19k points)

What is the most basic definition of "iterable", "iterator" and "iteration" in Python?

I have read multiple definitions but I am unable to identify the exact meaning as it still won't sink in.

Can someone please help me with the 3 definitions in layman terms?

2 Answers

0 votes
by (107k points)

When you work in Python, there is a specific meaning of iterable and iterator. Which is as follows:-

Iteration:-

So, the world iteration is a general term for taking each item of something(any lists, tuples etc), one after another. When you use a loop, to go over a group of items, that is called iteration.

Iterable:-

While, 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 an IndexErrorwhen the indexes are no longer valid). So an iterable is an object that you can get from an iterator.

Iterator:-

Both Python 2  and Python 3 has a method called  __next__. So an iterator is an object with a __next__ method.

So, whenever you use a for loop, or map, or a list comprehension, etc.  In Python, When going through the process of iteration the next method is automatically called to get each item from the iterator.

0 votes
by (20.3k points)

ITERABLE:

  • Anything that can be looped over (i.e. you can loop over a string or file) or
  • Anything that can appear on the right-side of a for-loop: for x in iterable: ... or
  • Anything you can call with iter() that will return an ITERATOR: iter(obj) or
  • An object that defines __iter__ that returns a fresh ITERATOR, or it may have a __getitem__ method Suitable for indexed lookup.

ITERATOR is an object:

  • With state that remembers where it is during iteration,
  • With a __next__ method that:
  • Returns the next value in the iteration
  • And updates the state to point at the next value
  • Signals when it is done by raising StopIteration
  • And that is self-iterable (meaning that it has an __iter__ method that returns self).

Notes:

  • The __next__ method in Python 3 is spelt next in Python 2, and
  • The builtin function next() calls that method on the object passed to it.

As for example:

>>> s = 'cat'      # s is an ITERABLE

                   # s is a str object that is immutable

                   # s has no state

                   # s has a __getitem__() method 

>>> t = iter(s)    # t is an ITERATOR

                   # t has state (it starts by pointing at the "c"

                   # t has a next() method and an __iter__() method

>>> next(t)        # the next() function returns the next value and advances the state

'c'

>>> next(t)        # the next() function returns the next value and advances

'a'

>>> next(t)        # the next() function returns the next value and advances

't'

>>> next(t)        # next() raises StopIteration to signal that iteration is complete

Traceback (most recent call last):

...

StopIteration

>>> iter(t) is t   # the iterator is self-iterable

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
0 votes
1 answer
asked Jul 8, 2019 in Python by Sammy (47.8k points)
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...