This text is part of a Real Python tutorial by Leodanis Pozo Ramos.
Understanding iterators and iterables in Python is crucial for running efficient iterations. Iterators control loops, allowing you to traverse arbitrary data containers one item at a time. Iterables, on the other hand, provide the data that you want to iterate over. By mastering these concepts, you can create robust code that handles data efficiently, even when working with large datasets or infinite data streams.
Understanding Iteration in Python
When writing computer programs, you often need to repeat a given piece of code multiple times. To do this, you can follow one of the following approaches:
- Repeating the target code as many times as you need in a sequence
- Putting the target code in a loop that runs as many times as you need
The first approach has a few drawbacks. The most troublesome issue is the repetitive code itself, which is hard to maintain and not scalable. For example, the following code will print a greeting message on your screen three times:
greeting.py
print("Hello!")
print("Hello!")
print("Hello!")
If you run this script, then you’ll get 'Hello!'
printed on your screen three times. This code works. However, what if you decide to update your code to print 'Hello, World!'
instead of just 'Hello!'
? In that case, you’ll have to update the greeting message three times, which is a maintenance burden.
Now imagine a similar situation but with a larger and more complex piece of code. It can become a nightmare for maintainers.
Using a loop will be a much better way to solve the problem and avoid the maintainability issue. Loops allow you to run a piece of code as often as you need. Consider how you’d write the above example using a while
loop:
>>> times = 0
>>> while times < 3:
... print("Hello!")
... times += 1
...
Hello!
Hello!
Hello!
This while
loop runs as long as the loop-continuation condition (times < 3
) remains true. In each iteration, the loop prints your greeting message and increments the control variable, times
. Now, if you decide to update your message, then you just have to modify one line, which makes your code way more maintainable.
Python’s while
loop supports what’s known as indefinite iteration, which means executing the same block of code over and over again, a potentially undefined number of times.
You’ll also find a different but similar type of iteration known as definite iteration, which means going through the same code a predefined number of times. This kind of iteration is especially useful when you need to iterate over the items of a data stream one by one in a loop.
To run an iteration like this, you typically use a for
loop in Python:
>>> numbers = [1, 2, 3, 4, 5]
>>> for number in numbers:
... print(number)
...
1
2
3
4
5
In this example, the numbers
list represents your stream of data, which you’ll generically refer to as an iterable because you can iterate over it, as you’ll learn later in this tutorial. The loop goes over each value in numbers
and prints it to your screen.
When you use a while
or for
loop to repeat a piece of code several times, you’re actually running an iteration. That’s the name given to the process itself.
In Python, if your iteration process requires going through the values or items in a data collection one item at a time, then you’ll need another piece to complete the puzzle. You’ll need an iterator.
Getting to Know Python Iterators
Iterators were added to Python 2.2 through PEP 234. They were a significant addition to the language because they unified the iteration process and abstracted it away from the actual implementation of collection or container data types. This abstraction allows iteration over unordered collections, such as sets, ensuring every element is visited exactly once.
In the following sections, you’ll get to know what a Python iterator is. You’ll also learn about the iterator protocol. Finally, you’ll learn when you might consider using iterators in your code.
What Is an Iterator in Python?
In Python, an iterator is an object that allows you to iterate over collections of data, such as lists, tuples, dictionaries, and sets.