Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly are iterator, iterable, and iteration?

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?

like image 602
thechrishaddad Avatar asked Mar 27 '12 06:03

thechrishaddad


People also ask

What is iterator vs iterable?

An Iterable is basically an object that any user can iterate over. An Iterator is also an object that helps a user in iterating over another object (that is iterable). We can generate an iterator when we pass the object to the iter() method.

What is iterable iterator?

Iterable is an object, that one can iterate over. It generates an Iterator when passed to iter() method. An iterator is an object, which is used to iterate over an iterable object using the __next__() method. Iterators have the __next__() method, which returns the next item of the object.

Is an iterator also an iterable?

An iterator is an object that implements the __iter__ method which returns itself and the __next__ method which returns the next element. Iterators are also iterables. However, they're iterables that become exhausted while iterables will never exhausted.

Why is iterator and iterable?

An Iterable is a simple representation of a series of elements that can be iterated over. It does not have any iteration state such as a "current element". Instead, it has one method that produces an Iterator . An Iterator is the object with iteration state.


2 Answers

Here's the explanation I use in teaching Python classes:

An ITERABLE is:

  • 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.

An 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
    • 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.

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 
like image 30
Raymond Hettinger Avatar answered Oct 03 '22 05:10

Raymond Hettinger


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 an IndexError 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, or map, or a list comprehension, etc. in Python, the next 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.

like image 145
agf Avatar answered Oct 03 '22 05:10

agf