What Is Iterable And Iterator In Python

  1. iterable
  2. iterator

1. BUT FIRST, ITERATION

  • iteration — of course — is taking items one by one from a source and doing something with each in turn
  • in python, this is commonly used in a) for/while loops and b) comprehensions
  • these structures iterate over the whole structure by default
  • sometimes, however, a more fine-grained control could be needed — like in generators
  • for this, there are 2 important concepts/protocols, on top of which much of Python is constructed:
  • a) iterable objects
  • b) iterator objects
  • both are reflected in standard python protocols
  • this is not something extra: actually, for/while loops and comprehensions are built directly upon these lower-level elements of iteration protocols

2. ITER() METHOD CREATES AN ITERATOR FROM AN ITERABLE

  • iterable object (collection or stream of objects) is any object that can be passed into the built-in iter() function
  • once passed the built-in iter() function and which returns an iterator object of a passed type, i.e. a string iterator is created with
>>> example_iterator = iter('abc')
>>> example_iterator
<str_iterator object at 0x063DCE38>
  • note that iterator is an implicit sequence object providing sequential (not random!) access to an underlying sequential dataset
  • for example range object itself is not an iterator — iterator does not allow the access to arbitrary element (`r) of the underlying series
  • they provide access only to the next element of the series
  • they provide sequential access
<!-- THIS IS NOT AN ITERATOR -->
>>> r = range(10)[5]
>>> r
5

3. NEXT() FUNCTION RETURNS THE NEXT VALUE FROM AN ITERATOR

  • the built-in next() requires an iterator object - and it returns the next value in the iteration of a collection
  • iterator consists of 2 components:
  1. mechanism for retrieving the next element of a collection
  2. mechanism for signalling the end of the series
  • next() allows to consider each item in turn / on request - not the whole series from beginning to an end
  • there are 2 messages iterator interface includes
  • next → query for the next element
  • iter → return the iterator
  • constraint: iterator can be iterated over once

4. CLASSROOM EXAMPLE — FROM ITERABLE TO ITERATOR TO STOPITERATION EXCEPTION

  • Python, liberally, raises an exception of the type StopIteration
iterable = ['Spring', 'Summer', 'Autumn', 'Winter']
iterator = iter(iterable)
next(iterator)
# >>> 'Spring'
next(iterator)
# >>> 'Summer'a r
next(iterator)
# >>> 'Autumn'
next(iterator)
# >>> 'Winter'
next(iterator)
# >>> Traceback (most recent call last):
# >>> File "<stdin>", line 1, in <module>
# StopIteration

5. REAL-LIFE EXAMPLE — UNIT TESTING MULTIPLE COMMAND LINE INPUTS

  1. define/get an iterable object such as a list ["20.01", "y"]
  2. pass an iterable object into iter() → create an iterator object
  3. pass an iterator object into a next() to yield the next value of the list each time the input function is called in the code
def test_pubDoc(monkeypatch):
inputs = iter(["20.01", "y"])
monkeypatch.setattr("builtins.input", lambda _: next(inputs))
#...
  • the first time input() is encountered, the "20.01" value is passed,
  • the second time it is "y"
  • the third time it would be an exception

6. LINKS

--

--

--

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Build a Production Ready Face Detection API

TeamCode x Merico, dive into an efficient cloud development era

Configure Sling Mapping for Resource Resolution in Adobe Experience Manager — Deep Dive

Resolving Network & USB Connectivity Issues in Parallels 16 for macOS Big Sur (“Network…

The foundation of Cudos and how it works

Microsoft Translator Service — Issue while fetching the Access Token | AEM(Adobe Experience…

Week 5: Internship Diary

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Pavol Kutaj

Pavol Kutaj

Infrastructure Support Engineer/Technical Writer (Snowplow Analytics) with a passion for Python/writing documentation. More about me: https://pavol.kutaj.com

More from Medium

Positional Only Arguments In Python

6 Ways You Can Get Keys From a Python Dictionary

Reverse-Engineered “Wordle” with 10 lines of Python

10 Useful String Operations in Python