What is an iterable?

Share
Copied to clipboard.
Series: Looping
Trey Hunner smiling in a t-shirt against a yellow wall
Trey Hunner
2 min. read Watch as video Python 3.8—3.12
Python Morsels
Watch as video
01:45

An iterable is anything you're able to iterate over (an iter-able).

Manual iteration using for loop

If you can write a for loop in Python to loop over something, that something is an iterable.

Here's a list, a tuple, a string, and a set:

>>> fruits = ['apple', 'lemon', 'pear', 'watermelon']
>>> coordinates = (1, 8, 2)
>>> greeting = "Hi y'all!"
>>> colors = {'red', 'blue', 'yellow'}

All of these are iterables.

The usual way to iterate over something is to write a for loop:

>>> for fruit in fruits:
...     print(fruit)
...
apple
lemon
pear
watermelon

As we loop over the fruits list above, we're assigning the variable fruit to each each item in the list and then doing something with it (printing in this case) inside the body of our loop.

The above for loop works, which means fruits is an iterable.

All iteration utilities do the same kind of looping

A for loop isn't the only way to iterate over an iterable.

For example, the list constructor does iteration as well. When we pass an iterable to the list constructor, it'll loop over it and make a new list out of its items:

>>> list(coordinates)
[1, 8, 2]

Here we've looped over a tuple (coordinates) and created a list out of it.

Strings are iterables too. As you loop over a string, you'll get each of the characters in that string:

>>> list(greeting)
['H', 'i', ' ', 'y', "'", 'a', 'l', 'l', '!']

Just like above, we're relying on the list constructor to do the looping (and list creating) for us.

Sets are also iterables, so we can loop over the colors set, either with a for loop or with a function (like the list constructor) that'll loop for us:

>>> colors = {'red', 'blue', 'yellow'}
>>> list(colors)
['yellow', 'blue', 'red']

Summary

We've seen that lists, tuples, sets, and strings are iterables.

Dictionaries, generators and files are also iterables. There are lots of other iterables in the Python, both built-in and included in third-party libraries.

Anything that you can write a for loop to loop over is an iterable.

Iterables might not have a length and you might not be able to index them. They might not even be finite (there are infinitely long iterables in Python)!

But if you can write a for loop to loop over something, it is an iterable.

Series: Looping

Unlike, JavaScript, C, Java, and many other programming languages we don't have traditional C-style for loops. Our for loops in Python don't have indexes.

This small distinction makes for some big differences in the way we loop in Python.

To track your progress on this Python Morsels topic trail, sign in or sign up.

0%
Concepts Beyond Intro to Python

Intro to Python courses often skip over some fundamental Python concepts.

Sign up below and I'll share ideas new Pythonistas often overlook.

Python Morsels
Watch as video
01:45