Typefully

Understanding… Iterable

Avatar

Share

 • 

3 years ago

 • 

View on X

––– Day 1 ––– ––– Iterable ––– Let’s start with iterable Easy 'definition' first: Any data type which can be used in a `for` loop
If all you want to know for now is how to determine whether something is an iterable or not, just put it at the end of a `for` statement and see whether you get an error
Let’s try it on some data types I’ll show these examples as code run in the Python Console (or REPL), the environment with the three chevron (angle bracket) prompt `>>>`
• Are lists iterable? >>> for item in [3, 4, 5]: ... print(item) ... 3 4 5 Yes, lists are iterable since there’s no error when you use them in a `for` loop
• Are integers iterable? >>> for item in 42: ... print(item) ... Traceback (most recent call last): ... TypeError: 'int' object is not iterable No, integers are not iterable
You can see the `TypeError` which states this clearly on the last line of the error message (or Traceback to use the fancy term)
• Are strings iterable? >>> for item in "Stephen": ... print(item) ... S t e p h e n Yes, strings are iterable
• Are dictionaries iterable? >>> for item in {"Name": "Stephen", "Favourite colour": None}: ... print(item) ... Name Favourite colour Yes, dictionaries are also iterable. When you loop through a dictionary you get the keys at each step of the iteration
[You can use `.items()` to get both keys and values. See the section Looping Through a Dictionary in Chapter 4 | Data, Data Types and Data Structures of The Python Coding Book] thepythoncodingbook.com/data-types-and-data-structures/
You can go ahead and try other data types…
A more detailed definition: An iterable is a Python object which can return its elements one at a time
You can always create an _iterator_ from an iterable. I’ll talk about iterators in another post in this series, so I won’t dwell on this term here
For now, just note that iterable and iterator are not the same term! You can convert an iterable into an iterator using the built-in function `iter()`
So, this is another way of checking whether an object of a certain type is iterable: pass it to `iter()` and see whether you get an iterator or an error:
In fact, when you use a `for` loop, the loop is converting the iterable into an iterator behind the scenes.
A class will create an iterable object if it has either `__iter__()` or `__getitem__()` defined as one of its special methods (also called dunder methods more informally)
––– Etymology Corner ––– Most English dictionaries won’t have “iterable” as a word The closest English words are “iterate” and “iteration” which we also use in programming, of course The word comes from Latin 'iterare' which means “to repeat”
More interestingly, the Latin 'iter' means “a journey” or “a path” So, when you iterate, you're going on a journey through the object, forging a path through its elements And the plural of 'iter' is 'itinera' from which we get the English word “itinerary”
And in English, words that end in "-able" are normally adjectives showing the ability to do something…
For example, something is: • "readable" if it can be read (_able_ to be read) • "knowable" if it can be known • "manageable" if it can be managed • "iterable" if it can be iterated
––– Tomorrow: Sequence –––
Avatar

Stephen Gruppetta

@s_gruppetta_ct

Constantly looking for innovative ways to talk and write about Python • Mentoring learners • Writing about Python • Writing about technical writing