Iterable: Python's Stepping Stones (Data Structure Categories #1)
What makes an iterable iterable? Part 1 of the Data Structure Categories Series
An iterable is an object you can iterate over
…is not a very useful definition of an iterable.
It's a bit like defining "combustible" as something that can combust or "razztwizzlerable" as an object you can razztwizzle. It's fine if you know the verbs "combust" or "razztwizzle". But they're still not great definitions.
Let's shed more light on this category of Python data types and see what makes an object iterable.
The Data Structure Categories Series
Before diving into iterables, here's a quick introduction to this series of articles. As you learn to code, you encounter data structures. Examples include lists, dictionaries, and tuples. You also come across the more general terms that describe categories of data types such as iterable, sequence, collection, and iterator—I could go on (excuse the pun!)
As you become more proficient with the programming fundamentals, you understand the data types better. However, the distinctions between the categories of data types can be a bit fuzzier. There's also a significant overlap between some of these definitions.
In this series of articles, I'll discuss some of these categories of data types. I won't try to cover all of them. Instead, I'll focus on the following:
I'll talk about sets in passing in this series, too.
What's an Iterable? The Short Version
The simplest definition of an iterable is an object you can loop through. Using a
for loop is one of the options. To find out if an object is iterable, put it at the end of a
for loop statement. If you get an error, it's not iterable.:
You use a list in the
for loop statement, and the loop works without errors. Therefore, lists are iterables. You can use them in a
for loop. You can iterate through them.
Next, try using an integer:
On this occasion, the code raises a
TypeError. The error message is helpful:
'int' object is not iterable. You cannot loop over an integer.
Let's try two more data types before moving on. Next up are strings:
for loop iterates over each character in the string. Therefore, strings are also iterable.
How about dictionaries?
Yes, dictionaries are also iterable. However, the loop only iterates over the keys of the dictionary. The values are ignored unless you explicitly loop over the key-value pairs using the
.items() dictionary method. You can read more about dictionaries and different ways of looping through them in Chapter 4 of The Python Coding Book about Data, Data Types, and Data Structures.
What's an Iterable? The More Detailed Version
An iterable is a Python object that can return its elements one at a time.
The terms "iterable" and "iterator" can easily be confused. They're different but also closely related. As I described earlier, an iterable is an object you can loop through. On the other hand, an iterator is an object that helps you go through the elements of an iterable one by one, keeping track of where you are in the process. I'll expand on iterators in a later article in this series.
You can always create an iterator from an iterable. There's a Python built-in function for this:
So, here's another test to check whether an object is iterable. Pass it as an argument to
iter(). The object is iterable if you don't get an error:
The list, string, and dictionary return some value. It doesn't matter what object is returned for now. The integer raises a
TypeError showing it's the wrong type of data to be used in
iter(). These are the same results you got when you used these objects in a
for loop. This is no coincidence since the
for loop creates an iterator from the iterable behind the scenes.
You may have spotted that for all the objects that returned a value without an error, the cryptic output contains the word "iterator". I'll cover iterators in detail in a later article.
What Makes an Object Iterable?
Every object in Python is a member of a class. When you define a class, you can define several special methods. These methods give the members of the class specific properties, and their names start and end with double underscores. I'll write more about classes and special methods on The Stack soon.
__getitem__() is defined in a class, the instances of the class are iterable.
The special method
__iter__() is the newer and preferred way of making objects of a class iterable. This special method defines how to create an iterator from the object and is called when you pass an object to the built-in function
iter() we discussed earlier. The special method
__getitem__() defines the object's behaviour when indexed with square brackets, such as
some_object. I’ll write more about both special methods in future articles.
The closest words to "iterable" that we use in plain English are "iterate" and "iteration". The ending "-able" is commonly used in English to convert verbs to adjectives that show the ability to perform that action. For example, "readable" means you're able to read it, "knowable" means you can know it, and "manageable" means you can manage it. Therefore, "iterable" means you can iterate over it.
Here are some more things worth knowing about the word "iterable":
Yes, you can use "iterable" as a noun and an adjective: a list is an iterable (noun) and it's iterable (adjective)
You can "iterate over" an iterable or "iterate through" an iterable. It doesn't matter. I'd say "to iterate over" is more common, but I've used both in this article!
The word "iterable" comes from the Latin iterare, which means "to repeat"
The Latin iter means "a journey" or "a path". Therefore, you can think of iteration as a journey through the object, forging a path through its elements
The plural of iter is itinera in Latin, from which we get "itinerary"
Code in this article uses Python 3.11
I'll be experimenting with publishing some articles without sending them out as emails. You'll still be able to read all the articles on the web platform at The Python Coding Stack, of course, but this way I won't fill up your email inboxes too much. The articles I plan to publish without emailing are:
Articles that are part of a series. I'll send the first and last in the series by email, and possibly one in the middle. However, the rest of the articles in the series will be published on The Stack without emailing them out. There will be links to them in the last post in the series so you can still get to them from your email inbox once I publish the final article in the series
Articles that are specifically targeted for beginners. Most of my audience is likely to be intermediate learners looking to dive deeper into Python programming topics. However, I know there are many beginners who are keen to read ahead and start to find out about more advanced topics. I'll write some articles specifically for beginners which will be published in The Stack but not emailed out. I'll include links to these articles in the Stop Stack section in the following article that's emailed out to everyone.
For those who are beginners or who know anyone who's starting to learn to code and wants a thorough live course with mentoring, the first two cohorts of The Python Coding Programme start soon–the first one starts in a few days' time. Live sessions with very small cohorts over 3 weeks, with 90 minutes live on Zoom every day (4 days a week). Each cohort only has 4 participants and there's active mentoring throughout with a private forum for the cohort to continue discussions. Here's some more information about The Python Coding Programme for Beginners.
Some of you are new to my writing, others have followed me on Twitter for a while. In these first weeks, you'll see some ideas discussed in these articles that you may have seen in my Twitter threads in recent weeks and months. In the past, I have generally used Twitter as the first platform on which I publish new content in short form and then expand into long or longer-form articles elsewhere.
Moving forward, new content will appear here on The Stack first. This is the home for all my new ideas and writing now. I'll mostly use Twitter for much shorter content and article summaries. However, during this "transition" period, I still have content that debuted on Twitter in the recent past that needs to be written out in full!
I'm spending more time on Substack's Notes? This is the new platform that looks like Twitter but, so far, has a different feel and tone. Do come an engage on the conversations there.