
At some point, someone may have dropped the word “iterate” or “iterable” in a sentence. Iteration is something that happens natively within Python when you’re writing loops
But what does this term mean and how is it used in the context of programming? Let’s talk about it.
What is iteration?
By definition, to iterate means to repeat a process, action, or set of instructions again and again. In the context of software engineering, we often use this term when we’re looping.
Take the following for loop that you’ve likely seen and used before:
for i in range(0, 10):
print(i)It’s straight-forward: take each value between 0 and 9 and print it out. If we were to dissect this snippet of code:
foris the syntax that tells the interpreter that we’re going to loop. We can think of this as our driver to iterate, or loop.range(0, 10)is the item that we are looping over - also known as the iterator.
» Reminder: the range function is a generator - check out what a generator is here. It’s useful to have context of what this is before proceeding.
So, in the context of software, there’s 3 slight variations of the word “iteration” which are tied closely together:
Iteration is when the code loop over an item.
Iterable is the item that you are looping over.
Iterator is an object that works under the hood that tells
foreach value within our iterable until there’s nothing left.
Examples of iterables
I can guarantee you you’ve written iterables multiple times in the past without knowing.
If you’ve ever created a list and then looped over that list, you’ve created an iterable! There are dozens of built-in iterables: sets, lists, dictionaries, generators:
def iterate(itm):
for i in itm:
print(i)
# List
example_list = [1, 2, 3]
iterate(example_list)
# Set
example_set = set([1, 2, 3])
iterate(example_set)
# Dictionary
example_dict = {'one' : 1, 'two' : 2}
iterate(example_dict)
# Generators; calling directly
iterate(range(-10, 20))Golden rule of thumb: anything that you can loop over is considered an iterable.
Honorable mention: itertools package
I’ve written about this in the past as an overlooked package due to it’s usefulness, but it deserves a mention here because it’s a library built specifically for common use cases where you have to loop over something.
For instance, if you need to go cycle through a list and go back to the starting point, you’d use cycle :
import matplotlib.pyplot as plt
from itertools import cycle
import random
# Set up a cycle for a list of colors to plot
colors = ['green', 'red', 'blue', 'purple']
color_cycle = cycle(colors)
x_vals = list(range(0, 10))
y_vals = [random.randint(0, 10) for _ in x_vals]
for x, y in zip(x_vals, y_vals):
plt.scatter(x, y, color = next(color_cycle))You may see a permutation question in coding interviews. Instead of spending time building the permutation, use the permutations class instead:
from itertools import permutations
# All permutations of length 2
perms = permutations('AB')
print(list(perms))
# Output: [('A', 'B'), ('B', 'A')]Happy coding!
📧 Join the Python Snacks Newsletter! 🐍
Want even more Python-related content that’s useful? Here’s 3 reasons why you should subscribe the Python Snacks newsletter:
Get Ahead in Python with bite-sized Python tips and tricks delivered straight to your inbox, like the one above.
Exclusive Subscriber Perks: Receive a curated selection of up to 6 high-impact Python resources, tips, and exclusive insights with each email.
Get Smarter with Python in under 5 minutes. Your next Python breakthrough could just an email away.
You can unsubscribe at any time.
Interested in starting a newsletter or a blog?
Do you have a wealth of knowledge and insights to share with the world? Starting your own newsletter or blog is an excellent way to establish yourself as an authority in your field, connect with a like-minded community, and open up new opportunities.
If TikTok, Twitter, Facebook, or other social media platforms were to get banned, you’d lose all your followers. This is why you should start a newsletter: you own your audience.
This article may contain affiliate links. Affiliate links come at no cost to you and support the costs of this blog. Should you purchase a product/service from an affiliate link, it will come at no additional cost to you.

