An iterable is anything you're able to iterate over (an iter-able).
for
loopIf you can write a for
loop to loop over something in Python, 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.
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:
>>> list(colors)
['yellow', 'blue', 'red']
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.
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.
Hello friendly web visitor! 👋
This page is part of Python Morsels, an online Python skill-building service.
The best way to learn is by doing. In the case of Python that means writing Python code. If you'd like to improve your Python skills every week, try out Python Morsels by entering your email below to create an account.
Python Morsels topics pages are free and the first month's worth of exercises is free as well. You don't need to enter payment details to sign up.
You can find explanations of many other Python topics by signing up below.
By signing up, you agree to the Privacy Policy.
Intro to Python courses often skip over some fundamental Python concepts.
Sign up below and I'll share ideas new Pythonistas often overlook.