Recommended Video Course
Efficient Iterations With Python Iterators and Iterables
Table of Contents
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Efficient Iterations With Python Iterators and Iterables
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.
By the end of this tutorial, you’ll understand that:
.__iter__()
and.__next__()
methods..__iter__()
method.yield
statement to creategenerator iterators..__aiter__()
and.__anext__()
for async operations.Before diving deeper into these topics, you should be familiar with some core concepts likeloops and iteration,object-oriented programming,inheritance,special methods, andasynchronous programming in Python.
Free Sample Code:Click here to download the free sample code that shows you how to use and create iterators and iterables for more efficient data processing.
Take the Quiz: Test your knowledge with our interactive “Iterators and Iterables in Python: Run Efficient Iterations” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Iterators and Iterables in Python: Run Efficient IterationsIn this quiz, you'll test your understanding of Python's iterators and iterables. By working through this quiz, you'll revisit how to create and work with iterators and iterables, the differences between them, and review how to use generator functions.
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:
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 yourun 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 awhile
loop:
>>>times=0>>>whiletimes<3:...print("Hello!")...times+=1...Hello!Hello!Hello!
Thiswhile
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’swhile
loop supports what’s known asindefinite 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 asdefinite 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 afor
loop in Python:
>>>numbers=[1,2,3,4,5]>>>fornumberinnumbers:...print(number)...12345
In this example, thenumbers
list represents your stream of data, which you’ll generically refer to as aniterable because you can iterate over it, as you’ll learn later in this tutorial. The loop goes over each value innumbers
and prints it to your screen.
When you use awhile
orfor
loop to repeat a piece of code several times, you’re actually running aniteration. 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 aniterator.
Iterators were added to Python2.2 throughPEP 234. They were a significant addition to the language because they unified the iteration process andabstracted it away from the actual implementation of collection orcontainer 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.
In Python, an iterator is an object that allows you to iterate over collections of data, such aslists, tuples,dictionaries, andsets.
Python iterators implement theiterator design pattern, which allows you to traverse a container and access its elements. The iterator pattern decouples the iterationalgorithms from containerdata structures.
Iterators take responsibility for two main actions:
In summary, an iterator will yield each item or value from a collection or a stream of data while doing all the internal bookkeeping required to maintain the state of the iteration process.
Python iterators must implement a well-established internal structure known as theiterator protocol. In the following section, you’ll learn the basics of this protocol.
A Python object is considered an iterator when it implements twospecial methods collectively known as theiterator protocol. These two methods make Python iterators work. So, if you want to create custom iterator classes, then you must implement the following methods:
Method | Description |
---|---|
.__iter__() | Called to initialize the iterator. It must return an iterator object. |
.__next__() | Called to iterate over the iterator. It must return the next value in the data stream. |
The.__iter__()
method of an iterator typicallyreturnsself
, which holds a reference to the current object: the iterator itself. This method is straightforward to write and, most of the time, looks something like this:
def__iter__(self):returnself
The only responsibility of.__iter__()
is to return an iterator object. So, this method will typically just returnself
, which holds the current instance. Don’t forget that this instancemust define a.__next__()
method.
The.__next__()
method will be a bit more complex depending on what you’re trying to do with your iterator. This method must return the next item from the data stream. It should also raise aStopIteration
exception when no more items are available in the data stream. This exception will make the iteration finish. That’s right. Iterators useexceptions forcontrol flow.
The most generic use case of a Python iterator is to allow iteration over a stream of data or a container data structure. Python uses iterators under the hood to support every operation that requires iteration, includingfor
loops,comprehensions,iterable unpacking, and more. So, you’re constantly using iterators without being conscious of them.
In your day-to-day programming, iterators come in handy when you need to iterate over a dataset or data stream with an unknown or a huge number of items. This data can come from different sources, such as your local disk, a database, and a network.
In these situations, iterators allow you to process the datasets one item at a time without exhausting the memory resources of your system, which is one of the most attractive features of iterators.
Using the two methods that make up the iterator protocol in your classes, you can write at least three different types of custom iterators. You can have iterators that:
The first kind of iterator is what you’d call aclassic iterator because it implements the original iterator pattern. The second and third types of iterators take the pattern further by adding new capabilities and leveraging the power of iterators.
Note: The second and third types of iterators may bring to mind some techniques that sound similar tomapping andfiltering operations fromfunctional programming.
In the following sections, you’ll learn how to use the iterator protocol to create iterators of all three different types.
Okay, now it’s time to learn how to write your own iterators in Python. As a first example, you’ll write a classic iterator calledSequenceIterator
. It’ll take asequence data type as an argument and yield its items on demand.
Fire up your favoritecode editor or IDE and create the following file:
sequence_iter.py
classSequenceIterator:def__init__(self,sequence):self._sequence=sequenceself._index=0def__iter__(self):returnselfdef__next__(self):ifself._index<len(self._sequence):item=self._sequence[self._index]self._index+=1returnitemelse:raiseStopIteration
YourSequenceIterator
will take a sequence of values atinstantiation time. The classinitializer,.__init__()
, takes care of creating the appropriateinstance attributes, including the input sequence and an._index
attribute. You’ll use this latter attribute as a convenient way to walk through the sequence using indices.
Note: You’ll note that the instance attributes in this and the next examples arenon-public attributes whose names start with anunderscore (_
). That’s because you don’t need direct access to those attributes from outside the class.
The.__iter__()
method does only one thing: returns the current object,self
. In this case,self
is the iterator itself, which implies it has a.__next__()
method.
Finally, you have the.__next__()
method. Inside it, you define aconditional statement to check if the current index is less than the number of items in the input sequences. This check allows you to stop the iteration when the data is over, in which case theelse
clause will raise aStopIteration
exception.
In theif
clause, you grab the current item from the original input sequence using its index. Then you increment the._index
attribute using anaugmented assignment. This action allows you to move forward in the iteration while you keep track of the visited items. The final step is to return the current item.
Here’s how your iterator works when you use it in afor
loop:
>>>fromsequence_iterimportSequenceIterator>>>foriteminSequenceIterator([1,2,3,4]):...print(item)...1234
Great! Your custom iterator works as expected. It takes a sequence as an argument and allows you to iterate over the original input data.
Note: You can create an iterator that doesn’t define an.__iter__()
method, in which case its.__next__()
method will still work. However, you must implement.__iter__()
if you want your iterator to work infor
loops. This loop always calls.__iter__()
to initialize the iterator.
Before diving into another example, you’ll learn how Python’sfor
loops work internally. The following code simulates the complete process:
>>>sequence=SequenceIterator([1,2,3,4])>>># Get an iterator over the data>>>iterator=sequence.__iter__()>>>whileTrue:...try:...# Retrieve the next item...item=iterator.__next__()...exceptStopIteration:...break...else:...# The loop's code block goes here......print(item)...1234
After instantiatingSequenceIterator
, the code prepares thesequence
object for iteration by calling its.__iter__()
method. This method returns the actual iterator object. Then, the loop repeatedly calls.__next__()
on the iterator to retrieve values from it.
Note: You shouldn’t use.__iter__()
and.__next__()
directly in your code. Instead, you should use the built-initer()
andnext()
functions, which fall back to calling the corresponding special methods.
When a call to.__next__()
raises theStopIteration
exception, you break out of the loop. In this example, the call toprint()
under theelse
clause of thetry
block represents the code block in a normalfor
loop. As you can see, thefor
loop construct is a kind ofsyntactic sugar for a piece of code like the one above.
Say that you want to write an iterator that takes a sequence of numbers, computes the square value of each number, and yields those values on demand. In this case, you can write the following class:
square_iter.py
classSquareIterator:def__init__(self,sequence):self._sequence=sequenceself._index=0def__iter__(self):returnselfdef__next__(self):ifself._index<len(self._sequence):square=self._sequence[self._index]**2self._index+=1returnsquareelse:raiseStopIteration
The first part of thisSquareIterator
class is the same as yourSequenceIterator
class. The.__next__()
method is also pretty similar. The only difference is that before returning the current item, the method computes its square value. This computation performs a transformation on each data point.
Here’s how the class will work in practice:
>>>fromsquare_iterimportSquareIterator>>>forsquareinSquareIterator([1,2,3,4,5]):...print(square)...1491625
Using an instance ofSquareIterator
in afor
loop allows you to iterate over the square values of your original input values.
The option of performing data transformation on a Python iterator is a great feature. It can make your code quite efficient in terms of memory consumption. Why? Well, imagine for a moment that iterators didn’t exist. In that case, if you wanted to iterate over the square values of your original data, then you’d need to create a new list to store the computed squares.
This new list would consume memory because it would have to store all the data simultaneously. Only then would you be able to iterate over the square values.
However, if you use an iterator, then your code will only require memory for a single item at a time. The iterator will compute the following items on demand without storing them in memory. In this regard, iterators arelazy objects.
You can also create custom iterators that generate a stream of new data from a given computation without taking a data stream as input. Consider the following iterator, which producesFibonacci numbers:
fib_iter.py
classFibonacciIterator:def__init__(self,stop=10):self._stop=stopself._index=0self._current=0self._next=1def__iter__(self):returnselfdef__next__(self):ifself._index<self._stop:self._index+=1fib_number=self._currentself._current,self._next=(self._next,self._current+self._next,)returnfib_numberelse:raiseStopIteration
This iterator doesn’t take a data stream as input. Instead, it generates each item by performing a computation that yields values from the Fibonacci sequence. You do this computation inside the.__next__()
method.
You start this method with a conditional that checks if the current sequence index hasn’t reached the._stop
value, in which case you increment the current index to control the iteration process. Then you compute the Fibonacci number that corresponds to the current index, returning the result to the caller of.__next__()
.
When._index
grows to the value of._stop
, you raise aStopIteration
, which terminates the iteration process. Note that you should provide a stop value when you call theclass constructor to create a new instance. Thestop
argumentdefaults to10
, meaning the class will generate ten Fibonacci numbers if you create an instance without arguments.
Here’s how you can use thisFibonacciIterator
class in your code:
>>>fromfib_iterimportFibonacciIterator>>>forfib_numberinFibonacciIterator():...print(fib_number)...0112358132134
Instead of yielding items from an existing data stream, yourFibonacciIterator
class computes every new value in real time, yielding values on demand. Note that in this example, you relied on the default number of Fibonacci numbers, which is10
.
An interesting feature of Python iterators is that they can handle potentially infinite data streams. Yes, you can create iterators that yield values without ever reaching an end! To do this, you just have to skip theStopIteration
part.
For example, say that you want to create a new version of yourFibonacciIterator
class that can produce potentially infinite Fibonacci numbers. To do that, you just need to remove theStopIteraton
and the condition that raises it:
inf_fib.py
classFibonacciInfIterator:def__init__(self):self._index=0self._current=0self._next=1def__iter__(self):returnselfdef__next__(self):self._index+=1self._current,self._next=(self._next,self._current+self._next)returnself._current
The most relevant detail in this example is that.__next__()
never raises aStopIteration
exception. This fact turns the instances of this class into potentiallyinfinite iterators that would produce values forever if you used the class in afor
loop.
Note: Infinite loops will cause your code tohang. To stop a program that’s entered an unexpected infinite loop, you may need to use your operating system’s tools, such as a task manager, to terminate the program’s execution.
If you’re working in a Python interactiveREPL, then you can press theCtrl+C key combination, which raises aKeyboardInterrupt
exception and terminates the loop.
To check if yourFibonacciInfIterator
works as expected, go ahead and run the following loop. But remember, it’ll be an infinite loop:
>>>frominf_fibimportFibonacciInfIterator>>>forfib_numberinFibonacciInfIterator():...print(fib_number)...0112358132134Traceback (most recent call last):...KeyboardInterrupt
When you run this loop in your Pythoninteractive session, you’ll notice that the loop prints numbers from the Fibonacci sequence without stopping. To stop the loops, go ahead and pressCtrl+C.
As you’ve confirmed in this example, infinite iterators likeFibonacciInfIterator
will makefor
loops run endlessly. They’ll also cause functions that accept iterators—such assum()
,max()
, andmin()
—to never return. So, be careful when using infinite iterators in your code, as you can make your code hang.
collections.abc.Iterator
Thecollections.abc
module includes anabstract base class (ABC) calledIterator
. You can use this ABC to create your custom iterators quickly. This class provides basic implementations for the.__iter__()
method.
It also provides a.__subclasshook__()
class method that ensures only classes implementing the iterator protocol will be considered subclasses ofIterator
.
Check out the following example, in which you change yourSequenceIterator
class to use theIterator
ABC:
sequence_iter.py
fromcollections.abcimportIteratorclassSequenceIterator(Iterator):def__init__(self,sequence):self._sequence=sequenceself._index=0def__next__(self):ifself._index<len(self._sequence):item=self._sequence[self._index]self._index+=1returnitemelse:raiseStopIteration
If you inherit fromIterator
, then you don’t have to write an.__iter__()
method because the superclass already provides one with the standard implementation. However, you do have to write your own.__next__()
method because the parent class doesn’t provide a working implementation.
Here’s how this class works:
>>>fromsequence_iterimportSequenceIterator>>>fornumberinSequenceIterator([1,2,3,4]):...print(number)...1234
As you can see, this new version ofSequenceIterator
works the same as your original version. However, this time you didn’t have to code the.__iter__()
method. Your class inherited this method fromIterator
.
The features inherited from theIterator
ABC are useful when you’re working with class hierarchies. They’ll take work off your plate and save you headaches.
Generator functions are special types offunctions that allow you to create iterators using a functional style. Unlike regular functions, which typically compute a value and return it to the caller, generator functions return agenerator iterator that yields a stream of data one value at a time.
Note: In Python, you’ll commonly use the termgenerators to collectively refer to two separate concepts: thegenerator function and thegenerator iterator. The generator function is the function that you define using theyield
statement. The generator iterator is what this function returns.
A generator function returns an iterator that supports the iterator protocol out of the box. So, generators are also iterators. In the following sections, you’ll learn how to create your own generator functions.
To create a generator function, you must use theyield
keyword to yield the values one by one. Here’s how you can write a generator function that returns an iterator that’s equivalent to yourSequenceIterator
class:
>>>defsequence_generator(sequence):...foriteminsequence:...yielditem...>>>sequence_generator([1,2,3,4])<generator object sequence_generator at 0x108cb6260>>>>fornumberinsequence_generator([1,2,3,4]):...print(number)...1234
Insequence_generator()
, you accept a sequence of values as an argument. Then you iterate over that sequence using afor
loop. In each iteration, the loop yields the current item using theyield
keyword. This logic is then packed into a generator iterator object, which automatically supports the iterator protocol.
You can use this iterator in afor
loop as you would use aclass-based iterator. Internally, the iterator will run the original loop, yielding items on demand until the loop consumes the input sequence, in which case the iterator will automatically raise aStopIteration
exception.
Generator functions are a great tool for creatingfunction-based iterators that save you a lot of work. You just have to write a function, which will often be less complex than a class-based iterator. If you comparesequence_generator()
with its equivalent class-based iterator,SequenceIterator
, then you’ll note a big difference between them. The function-based iterator is way simpler and more straightforward to write and understand.
If you like generator functions, then you’ll lovegenerator expressions. These are particular types of expressions that return generator iterators. The syntax of a generator expression is almost the same as that of a list comprehension. You only need to turn the square brackets ([]
) into parentheses:
>>>[itemforitemin[1,2,3,4]]# List comprehension[1, 2, 3, 4]>>>(itemforitemin[1,2,3,4])# Generator expression<generator object <genexpr> at 0x7f55962bef60>>>>generator_expression=(itemforitemin[1,2,3,4])>>>foritemingenerator_expression:...print(item)...1234
Wow! That was really neat! Your generator expression does the same as its equivalent generator function. The expression returns a generator iterator that yields values on demand. Generator expressions are an amazing tool that you’ll probably use a lot in your code.
As you’ve already learned, classic iterators typically yield data from an existing iterable, such as a sequence or collection data structure. The examples in the above section show that generators can do just the same.
However, as their name suggests, generators cangenerate streams of data. To do this, generators may or may not take input data. Like class-based iterators, generators allow you to:
To illustrate the second use case, check out how you can write an iterator of square values using a generator function:
>>>defsquare_generator(sequence):...foriteminsequence:...yielditem**2...>>>forsquareinsquare_generator([1,2,3,4,5]):...print(square)...1491625
Thissquare_generator()
function takes a sequence and computes the square value of each of its items. Theyield
statement yields square items on demand. When you call the function, you get a generator iterator that generates square values from the original input data.
Alternatively, generators can just generate the data by performing some computation without the need for input data. That was the case with yourFibonacciIterator
iterator, which you can write as a generator function like the following:
>>>deffibonacci_generator(stop=10):...current_fib,next_fib=0,1...for_inrange(0,stop):...fib_number=current_fib...current_fib,next_fib=(...next_fib,current_fib+next_fib...)...yieldfib_number...>>>list(fibonacci_generator())[0, 1, 1, 2, 3, 5, 8, 13, 21, 34]>>>list(fibonacci_generator(15))[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377]
This functional version of yourFibonacciIterator
class works as expected, producing Fibonacci numbers on demand. Note how you’ve simplified the code by turning your iterator class into a generator function. Finally, to display the actual data, you’ve calledlist()
with the iterator as an argument. These calls implicitly consume the iterators, returning lists of numbers.
In this example, when the loop finishes, the generator iterator automatically raises aStopIteration
exception. If you want total control over this process, then you can terminate the iteration yourself by using anexplicitreturn
statement:
deffibonacci_generator(stop=10):current_fib,next_fib=0,1index=0whileTrue:ifindex==stop:returnindex+=1fib_number=current_fibcurrent_fib,next_fib=next_fib,current_fib+next_fibyieldfib_number
In this version offibonacci_generator()
, you use awhile
loop to perform the iteration. The loop checks the index in every iteration and returns when the index has reached thestop
value.
You’ll use areturn
statement inside a generator function to explicitly indicate that the generator is done. Thereturn
statement will make the generator raise aStopIteration
.
Just like class-based iterators, generators are useful when you need to process a large amount of data, including infinite data streams. You can pause and resume generators, and you can iterate over them. They generate items on demand, so they’re also lazy. Both iterators and generators are pretty efficient in terms of memory usage. You’ll learn more about this feature in the following section.
Iterators and generators are pretty memory-efficient when you compare them with regular functions, container data types, and comprehensions. With iterators and generators, you don’t need to store all the data in your compter’s memory at the same time.
Iterators and generators also allow you to completely decouple iteration from processing individual items. They let you connect multiple data processing stages to create memory-efficient data processingpipelines.
In the following sections, you’ll learn how to use iterators, specifically generator iterators, to process your data in a memory-efficient manner.
Regular functions and comprehensions typically create a container type like a list or a dictionary to store the data that results from the function’s intended computation. All this data is stored in memory at the same time.
In contrast, iterators keep only one data item in memory at a time, generating the next items on demand or lazily. For example, get back to the square values generator. Instead of using a generator function that yields values on demand, you could’ve used a regular function like the following:
>>>defsquare_list(sequence):...squares=[]...foriteminsequence:...squares.append(item**2)...returnsquares...>>>numbers=[1,2,3,4,5]>>>square_list(numbers)[1, 4, 9, 16, 25]
In this example, you have two list objects: the original sequence ofnumbers
and the list of square values that results from callingsquare_list()
. In this case, the input data is fairly small. However, if you start with a huge list of values as input, then you’ll be using a large amount of memory to store the original and resulting lists.
In contrast, if you use a generator, then you’ll only need memory for the input list and for a single square value at a time.
Similarly, generator expressions are more memory-efficient than comprehensions. Comprehensions create container objects, while generator expressions return iterators that produce items when needed.
Another important memory-related difference between iterators, functions, data structures, and comprehensions is that iterators are the only way to process infinite data streams. In this situation, you can’t use a function that creates a new container directly, because your input data is infinite, which will hang your execution.
As mentioned before, you can use iterators and generators to build memory-efficient data processing pipelines. Your pipeline can consist of multiple separate generator functions performing a single transformation each.
For example, say you need to perform a bunch of mathematical tranformations on a sample of integer numbers. You may need to raise the values to the power of two or three, filter even and odd numbers, and finally convert the data into string objects. You also need your code to be flexible enough that you can decide which specific set of transformations you need to run.
Here’s your set of individual generator functions:
math_pipeline.py
defto_square(numbers):return(number**2fornumberinnumbers)defto_cube(numbers):return(number**3fornumberinnumbers)defto_even(numbers):return(numberfornumberinnumbersifnumber%2==0)defto_odd(numbers):return(numberfornumberinnumbersifnumber%2!=0)defto_string(numbers):return(str(number)fornumberinnumbers)
All these functions take some sample data as theirnumbers
argument. Each function performs a specific mathematical transformation on the input data and returns an iterator that produces transformed values on demand.
Here’s how you can combine some of these generator functions to create different data processing pipelines:
>>>importmath_pipelineasmpl>>>list(mpl.to_string(mpl.to_square(mpl.to_even(range(20)))))['0', '4', '16', '36', '64', '100', '144', '196', '256', '324']>>>list(mpl.to_string(mpl.to_cube(mpl.to_odd(range(20)))))['1', '27', '125', '343', '729', '1331', '2197', '3375', '4913', '6859']
Your first pipeline takes some numbers, extracts the even ones, finds the square value of those even numbers, and finally converts each resulting value into a string object. Note how each function provides the required argument for the next function on the pipeline. The second pipeline works similarly.
Python iterators have several neat and useful features that make them amazing. Because of these features, iterators are a fundamental tool for most Python developers. Iterators allow you to:
However, iterators have a few constraints and limitations that you must remember when working with them in your Python code. The first and probably the most overlooked constraint is thatyou can’t iterate over an iterator more than once.
Consider the following code, which reuses yourSequenceIterator
class:
>>>fromsequence_iterimportSequenceIterator>>>numbers_iter=SequenceIterator([1,2,3,4])>>>fornumberinnumbers_iter:...print(number)...1234>>>fornumberinnumbers_iter:...print(number)...
The second loop in this example doesn’t print anything on your screen. The problem is that the first loopconsumed all the items from yournumbers_iter
iterator. Once you’ve consumed all the items from an iterator, that iterator isexhausted.
In this example, the iterator is exhausted when you start the second loop. An exhausted iterator’s only action is to raise aStopIteration
exception, which immediately terminates any loop.
This behavior leads to the second constraint:you can’t reset an exhausted iterator to start iteration again. If you want to iterate over the same data again, then you must create a new iterator:
>>>another_iter=SequenceIterator([1,2,3,4])>>>fornumberinanother_iter:...print(number)...1234
Here, you create a completely new iterator calledanother_iter
by instantiatingSequenceIterator
again. This new iterator allows you to iterate through the original data once again.
As you already learned, one of the responsibilities of an iterator is to keep track of the current and visited items in an iteration. Therefore, you can partially consume iterators. In other words, you can retrieve a definite number of items from an iterator and leave the rest untouched:
>>>numbers_iter=SequenceIterator([1,2,3,4,5,6])>>>fornumberinnumbers_iter:...ifnumber==4:...break...print(number)...123>>>next(numbers_iter)5>>>next(numbers_iter)6>>>next(numbers_iter)Traceback (most recent call last):...StopIteration
In this example, you use a conditional statement to break the loop when the current number equals4
. Now the loop only consumes the first four numbers innumbers_iter
. You can access the rest of the values using.__next__()
or a second loop. Note that there’s no way to access consumed values.
Another constraint of iterators is that they only define the.__next__()
method, which gets the next item each time. There’s no.__previous__()
method or anything like that. This means thatyou can only move forward through an iterator. You can’t move backward.
Because iterators only keep one item in memory at a time,you can’t know their length or number of items, which is another limitation. If your iterator isn’t infinite, then you’ll only know its length when you’ve consumed all its data.
Finally, unlike lists and tuples,iterators don’t allowindexing and slicing operations with the[]
operator:
>>>numbers_iter=SequenceIterator([1,2,3,4,5,6])>>>numbers_iter[2]Traceback (most recent call last):...TypeError:'SequenceIterator' object is not subscriptable>>>numbers_iter[1:3]Traceback (most recent call last):...TypeError:'SequenceIterator' object is not subscriptable
In the first example, you try to get the item at index2
innumbers_iter
, but you get aTypeError
because iterators don’t support indexing. Similarly, when you try to retrieve a slice of data fromnumbers_iter
, you get aTypeError
too.
Fortunately, you can create iterators that overcome some of the above constraints. For example, you can create an iterator that allows you to iterate over the underlying data multiple consecutive times:
reusable_range.py
classReusableRange:def__init__(self,start=0,stop=None,step=1):ifstopisNone:stop,start=start,0self._range=range(start,stop,step)self._iter=iter(self._range)def__iter__(self):returnselfdef__next__(self):try:returnnext(self._iter)exceptStopIteration:self._iter=iter(self._range)raise
ThisReusableRange
iterator mimics some of the core behavior of the built-inrange
class. The.__next__()
method creates a new iterator over therange
object every time you consume the data.
Here’s how this class works:
>>>fromreusable_rangeimportReusableRange>>>numbers=ReusableRange(10)>>>list(numbers)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]>>>list(numbers)[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Here, you instantiateReusableRange
to create a reusable iterator over a given range of numbers. To try it out, you calllist()
several times with thenumbers
iterator object as an argument. In all cases, you get a new list of values.
next()
FunctionThe built-innext()
function lets you retrieve the next item from an iterator. To do this,next()
automatically falls back to calling the iterator’s.__next__()
method. In practice, you shouldn’t call special methods like.__next__()
directly in your code, so if you need to get the next item from an iterator, then you should usenext()
.
Thenext()
function can play an important role in your code when you’re working with Python iterators. This function allows you to traverse an iterator without a formal loop. This possibility can be helpful when you’re working with infinite iterators or with iterators that have an unknown number of items.
A common use case ofnext()
is when you need to manually skip over the header line in aCSV file.File objects are also iterators that yield lines on demand. So, you can callnext()
with a CSV file as an argument to skip its first line and then pass the rest of the file to afor
loop for further processing:
withopen("sample_file.csv")ascsv_file:next(csv_file)forlineincsv_file:# Process file line by line here...print(line)
In this example, you use thewith
statement toopen a CSV file containing some target data. Because you just want to process the data, you need to skip the first line of the file, which contains headers for each data column rather than data. To do this, you callnext()
with the file object as an argument.
This call tonext()
falls back to the file object’s.__next__()
method, which returns the next line in the file. In this case, the next line is the first line because you haven’t started to consume the file.
You can also pass a second andoptional argument tonext()
. The argument is calleddefault
and allows you to provide a default value that’ll be returned when the target iterator raises theStopIteration
exception. So,default
is a way to skip the exception:
>>>numbers_iter=SequenceIterator([1,2,3])>>>next(numbers_iter)1>>>next(numbers_iter)2>>>next(numbers_iter)3>>>next(numbers_iter)Traceback (most recent call last):...StopIteration>>>numbers_iter=SequenceIterator([1,2,3])>>>next(numbers_iter,0)1>>>next(numbers_iter,0)2>>>next(numbers_iter,0)3>>>next(numbers_iter,0)0>>>next(numbers_iter,0)0
If you callnext()
without a default value, then your code will end with aStopIteration
exception. On the other hand, if you provide a suitable default value in the call tonext()
, then you’ll get that value as a result when the iterator gets exhausted.
So far, you’ve learned a lot about iterators in Python. It’s time for you to get intoiterables, which are slightly different tools.
When it comes to iteration in Python, you’ll often hear people talking aboutiterable objects or justiterables. As the name suggests, an iterable is an object that you caniterate over. To perform this iteration, you’ll typically use afor
loop.
Pure iterable objects typically hold the data themselves. For example, Python built-in container types—such as lists, tuples, dictionaries, and sets—are iterable objects. They provide a stream of data that you can iterate over. However, iterators are also iterable objects even if they don’t hold the data themselves. You’ll learn more about this fact in the sectionComparing Iterators vs Iterables.
Python expects iterable objects in several different contexts, the most important beingfor
loops. Iterables are also expected inunpacking operations and in built-in functions, such asall()
,any()
,enumerate()
,max()
,min()
,len()
,zip()
,sum()
,map()
, andfilter()
.
Other definitions of iterables include objects that:
iter()
function return an iteratorIn the following sections, you’ll learn about these three ways to define iterables in Python. To kick things off, you’ll start by understanding the iterable protocol.
The iterable protocol consists of a single special method that you already know from the section on the iterator protocol. The.__iter__()
method fulfills the iterable protocol. This method must return an iterator object, which usually doesn’t coincide withself
unless your iterable is also an iterator.
Note: Aniterable is an object implementing the.__iter__()
special method or the.__getitem__()
method as part of thesequence protocol. In this sense, you can think of iterables as a class implementing theiterable protocol, even if this term isn’t officially defined. In Python, aprotocol is a set of dunder methods that allows you to support a given feature in a class.
You already know.__iter__()
from the section on theiterator protocol. This method must return an iterator object, which usually doesn’t coincide withself
unless your iterable is also an iterator.
To quickly jump into an example of how the iterable protocol works, you’ll reuse theSequenceIterator
class from previous sections. Here’s the implementation:
sequence_iterable.py
fromsequence_iterimportSequenceIteratorclassIterable:def__init__(self,sequence):self.sequence=sequencedef__iter__(self):returnSequenceIterator(self.sequence)
In this example, yourIterable
class takes a sequence of values as an argument. Then, you implement an.__iter__()
method that returns an instance ofSequenceIterator
built with the input sequence. This class is ready for iteration:
>>>fromsequence_iterableimportIterable>>>forvalueinIterable([1,2,3,4]):...print(value)...1234
The.__iter__()
method is what makes an object iterable. Behind the scenes, the loop calls this method on the iterable to get an iterator object that guides the iteration process through its.__next__()
method.
Note that iterables aren’t iterators on their own. So, you can’t use them as direct arguments to thenext()
function:
>>>numbers=Iterable([1,2,3,4])>>>next(numbers)Traceback (most recent call last):...TypeError:'Iterable' object is not an iterator>>>letters="ABCD">>>next(letters)Traceback (most recent call last):...TypeError:'str' object is not an iterator>>>fruits=[..."apple",..."banana",..."orange",..."grape",..."lemon",...]>>>next(fruits)Traceback (most recent call last):...TypeError:'list' object is not an iterator
You can’t pass an iterable directly to thenext()
function because, in most cases, iterables don’t implement the.__next__()
method from the iterator protocol. This is intentional. Remember that the iterator pattern intends to decouple the iteration algorithm from data structures.
Note: You can add a.__next__()
method to a custom iterable and returnself
from its.__iter__()
method. This will turn your iterable into an iterator on itself. However, this addition imposes some limitations. The most relevant limitation may be that you won’t be able to iterate several times over your iterable.
So, when you create your own container data structures, make them iterables, but think carefully to decide if you need them to be iterators too.
Note how both custom iterables and built-in iterables, such as strings and lists, fail to supportnext()
. In all cases, you get aTypeError
telling you that the object at hand isn’t an iterator.
Ifnext()
doesn’t work, then how can iterables work infor
loops? Well,for
loops always call the built-initer()
function to get an iterator out of the target stream of data. You’ll learn more about this function in the next section.
iter()
FunctionFrom Python’s perspective, an iterable is an object that can be passed to the built-initer()
function to get an iterator from it. Internally,iter()
falls back to calling.__iter__()
on the target objects. So, if you want a quick way to determine whether an object is iterable, then use it as an argument toiter()
. If you get an iterator back, then your object is iterable. If you get an error, then the object isn’t iterable:
>>>fruits=[..."apple",..."banana",..."orange",..."grape",..."lemon",...]>>>iter(fruits)<list_iterator object at 0x105858760>>>>iter(42)Traceback (most recent call last):...TypeError:'int' object is not iterable
When you pass an iterable object, like a list, as an argument to the built-initer()
function, you get an iterator for the object. In contrast, if you calliter()
with an object that’s not iterable, like an integer number, then you get aTypeError
exception.
reversed()
FunctionPython’s built-inreversed()
function allows you to create an iterator that yields the values of an input iterable in reverse order. Аsiter()
falls back to calling.__iter__()
on the underlying iterable,reversed()
delegates on a special method called.__reverse__()
that’s present in ordered built-in types, such as lists, tuples, and dictionaries.
Other ordered types, such as strings, also supportreversed()
even though they don’t implement a.__reverse__()
method.
Here’s an example of howreversed()
works:
>>>digits=[0,1,2,3,4,5,6,7,8,9]>>>reversed(digits)<list_reverseiterator object at 0x1053ff490>>>>list(reversed(digits))[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
In this example, you usereversed()
to create an iterator object that yields values from thedigits
list in reverse order.
To do its job,reversed()
falls back to calling.__reverse__()
on the input iterable. If that iterable doesn’t implement.__reverse__()
, thenreversed()
checks the existence of.__len__()
and.__getitem___(index)
. If these methods are present, thenreversed()
uses them to iterate over the data sequentially. If none of these methods are present, then callingreversed()
on such an object will fail.
Sequences are container data types that store items in consecutive order. Each item is quickly accessible through a zero-based index that reflects the item’s relative position in the sequence. You can use this index and the indexing operator ([]
) to access individual items in the sequence:
>>>numbers=[1,2,3,4]>>>numbers[0]1>>>numbers[2]3
Integer indices give you access to individual values in the underlying list of numbers. Note that the indices start from zero.
All built-in sequence data types—like lists, tuples, and strings—implement the sequence protocol, which consists of the following methods:
.__getitem__(index)
takes an integer index starting from zero and returns the items at that index in the underlying sequence. It raises anIndexError
exception when the index is out of range..__len__()
returns the length of the sequence.When you use an object that supports these two methods, Python internally calls.__getitem__()
to retrieve each item sequentially and.__len__()
to determine the end of the data. This means that you can use the object in a loop directly.
Note: Python dictionaries also implement.__getitem__()
and.__len__()
. However, they’re considered mapping data types rather than sequences because the lookups use arbitraryimmutable keys rather than integer indices.
To check this internal behavior of Python, consider the following class, which implements a minimalstack data structure using a list to store the actual data:
stack.py
classStack:def__init__(self):self._items=[]defpush(self,item):self._items.append(item)defpop(self):try:returnself._items.pop()exceptIndexError:raiseIndexError("pop from an empty stack")fromNonedef__getitem__(self,index):returnself._items[index]def__len__(self):returnlen(self._items)
ThisStack
class provides the two core methods that you’ll typically find in a stack data structure. Thepush()
method allows you to add items to the top of the stack, while thepop()
method removes and returns items from the top of the stack. This way, you guarantee that your stack works according to the LIFO (last in, first out) principle.
The class also implements the Python sequence protocol. The.__getitem__()
method returns the item atindex
from the underlying list object,._items
. Meanwhile, the.__len__()
method returns the number of items in the stack using the built-inlen()
function.
These two methods make yourStack
class iterable:
>>>fromstackimportStack>>>stack=Stack()>>>stack.push(1)>>>stack.push(2)>>>stack.push(3)>>>stack.push(4)>>>iter(stack)<iterator object at 0x104e9b460>>>>forvalueinstack:...print(value)...1234
YouStack
class doesn’t have an.__iter__()
method. However, Python is smart enough to build an iterator using.__getitem__()
and.__len__()
. So, your class supportsiter()
and iteration. You’ve created an iterable without formally implementing the iterable protocol.
Up to this point, you’ve learned what an iterable is in Python and how to create them using different techniques. You’ve learned that iterables themselves contain the data. For example, lists, tuples, dictionaries, strings, and sets are all iterables.
Iterables are present in many contexts in Python. You’ll use them infor
loops, unpacking operations, comprehensions, and even as arguments to functions. They’re an important part of Python as a language.
In the following sections, you’ll explore how iterables work in most of the contexts mentioned before. To start off, you’ll use iterables in definite iteration.
for
LoopsIterables shine in the context of iteration. They’re especially useful indefinite iteration, which usesfor
loops to access the items in iterable objects. Python’sfor
loops are specially designed to traverse iterables. That’s why you can use iterables directly in this type of loop:
>>>fornumberin[1,2,3,4]:...print(number)...1234
Internally, the loop creates the required iterator object to control the iteration. This iterator keeps track of the item currently going through the loop. It also takes care of retrieving consecutive items from the iterable and finishing the iteration when the data is over.
Python also allows you to use iterables in another kind of iteration known as comprehensions. Comprehensions work similarly tofor
loops but have a more compact syntax.
You can use comprehensions to create new lists, dictionaries, and sets from existing iterables of data. In all cases, the comprehension construct will iterate over the input data, transform it, and generate a new container data type.
For example, say that you want to process a list of numeric values and create a new list with cube values. In this case, you can use the following list comprehension to perform the data transformation:
>>>numbers=[1,2,3,4]>>>[number**3fornumberinnumbers][1, 8, 27, 64]
This list comprehension builds a new list of cube values from the original data innumbers
. Note how the syntax of a comprehension resembles afor
loop with the code block at the beginning of the construct.
You can also use comprehensions to process iterables conditionally. To do this, you can add one or more conditions at the end of the comprehension construct:
>>>numbers=[1,2,3,4,5,6]>>>[numberfornumberinnumbersifnumber%2==0][2, 4, 6]
The condition at the end of this comprehension filters the input data, creating a new list with even numbers only.
Comprehensions are popular tools in Python. They provide a great way to process iterables of data quickly and concisely. To dive deeper into list comprehensions, check outWhen to Use a List Comprehension in Python.
In Python, you can use iterables in a type of operation known asiterable unpacking. Unpacking an iterable means assigning its values to a series of variables one by one. The variables must come as a tuple or list, and the number of variables must match the number of values in the iterable.
Iterable unpacking can help you write more concise, readable code. For example, you may find yourself doing something like this:
>>>numbers=[1,2,3,4]>>>one=numbers[0]>>>two=numbers[1]>>>three=numbers[2]>>>four=numbers[3]>>>one1>>>two2>>>three3>>>four4
If this is your case, then go ahead and replace the series of individualassignments with a more readable iterable unpacking operation using a single, elegant assignment like the following:
>>>numbers=[1,2,3,4]>>>one,two,three,four=numbers>>>one1>>>two2>>>three3>>>four4
In this example,numbers
is an iterable containing numeric data. The assignment unpacks the values innumbers
into the four target variables. To do this, Python internally runs a quick loop over the iterable on the right-hand side to unpack its values into the target variables.
Note: Because Pythonsets are also iterables, you can use them in an iterable unpacking operation. However, because sets are unordered data structures, it won’t be clear which value goes to which variable.
The above example shows the most common form of iterable unpacking in Python. The main condition for the example to work is that the number of variables must match the number of values in the iterable.
.__iter__()
in IterablesAs you’ve learned in previous sections, if you want an object to be iterable, then you’ll have to provide it with an.__iter__()
method that returns an iterator. That iterator must implement the iterator protocol, which requires the.__iter__()
and.__next__()
methods.
In this section, you’ll walk through a few alternative ways to create iterators using the standard iterable protocol. In other words, you’ll learn different ways to write your.__iter__()
methods and make your objects iterable. Note that you’ll typically define this method in classes that work as data containers or collections.
A quick way to create an.__iter__()
method is to take advantage of the built-initer()
function, which returns an iterator out of an input data stream. As an example, get back to yourStack
class and make the following changes to the code:
stack.py
classStack:def__init__(self):self._items=[]defpush(self,item):self._items.append(item)defpop(self):try:returnself._items.pop()exceptIndexError:raiseIndexError("pop from an empty stack")fromNonedef__iter__(self):returniter(self._items)
In this example, you useiter()
to get an iterator out of your original data stored in._items
. This is a quick way to write an.__iter__()
method. However, it’s not the only way to do it.
You can also turn your.__iter__()
method into a generator function using theyield
statement in a loop over._items
:
stack.py
classStack:# ...def__iter__(self):foriteminself._items:yielditem
Generator functions return an iterator object that yields items on demand. In this example, the items will come from your class’s._items
attribute, which holds the original data in the stack.
Finally, you can also use theyield from <iterable>
syntax, which was introduced in PEP380 as a quick way to create generator iterators:
stack.py
classStack:# ...def__iter__(self):yield fromself._items
This syntax is pretty concise and readable. It comes in handy when you need to yield items directly from an existing iterable, like in this example.
Up to this point, you’ve learned a lot about iterators and iterables in Python. When you’re beginning with Python, it’s common to run into errors because you confuse iterables and iterators. Iterables have an.__iter__()
method that produce items on demand. Iterators implement an.__iter__()
method that typically returnsself
and a.__next__()
method that returns an item in every call.
According to this internal structure, you can conclude that all iterators are iterables because they meet the iterable protocol. However, not all iterables are iterators—only those implementing the.__next__()
method.
The immediate consequence of this difference is that you can’t use pure iterables as arguments to thenext()
function:
>>>fruits=[..."apple",..."banana",..."orange",..."grape",..."lemon",...]>>>next(fruits)Traceback (most recent call last):...TypeError:'list' object is not an iterator>>>hello="Hello, World!">>>next(hello)Traceback (most recent call last):...TypeError:'str' object is not an iterator
When you callnext()
with an iterable as an argument, you get aTypeError
. This is because pure iterables don’t provide a.__next__()
special method that thenext()
function can internally call to retrieve the next data item.
In the above examples, you callnext()
with a list and a string object, respectively. These data types are iterables but not iterators, so you get errors.
It’s important to note that.__iter__()
is semantically different for iterables and iterators. In iterators, the method returns the iterator itself, which must implement a.__next__()
method. In iterables, the method should yield items on demand.
You may feel tempted to add a.__next__()
method to a custom iterable. This addition will make it an iterable and an iterator at the same time. However, this practice isn’t recommended because it prevents multiple iterations over the underlying data. To support multiple iterations through the data, you must be able to obtain multiple independent iterators from it.
With iterator objects, it’s unlikely that you’ll get a new iterator every time, because their.__iter__()
method typically returnsself
. This means that you’ll be getting the same iterator every time. In contrast, the.__iter__()
method of an iterable will return a new and different iterator object every time you call it.
Another difference has to do with the underlying data. Pure iterables typically hold the data themselves. In contrast, iterators don’t hold the data but produce it one item at a time, depending on the caller’s demand. Therefore, iterators are more efficient than iterables in terms of memory consumption.
Here’s a summary of the above and other differences between iterators and iterables in Python:
Feature | Iterators | Iterables |
---|---|---|
Can be used infor loops directly | ✅ | ✅ |
Can be iterated over many times | ❌ | ✅ |
Support theiter() function | ✅ | ✅ |
Support thenext() function | ✅ | ❌ |
Keep information about the state of iteration | ✅ | ❌ |
Optimize memory use | ✅ | ❌ |
The first feature in this list is possible because Python’sfor
loops always calliter()
to get an iterator out of the target data. If this call succeeds, then the loop runs. Otherwise, you get an error.
In general, when dealing with huge datasets, you should take advantage of iterators and write memory-efficient code. In contrast, if you’re coding custom container or collection classes, then provide them with the iterable protocol so that you can use them later infor
loops.
Concurrency andparallelism are expansive subjects in modern computing. Python has made multiple efforts in this direction. Since Python3.7, the language has included theasync
andawait
keywords and a complete standard-library framework calledasyncio
for asynchronous programming.
Note:Concurrency andparallelism are two popular topics in modern computer programming. Parallelism consists of performing multiple operations or tasks simultaneously by taking advantage of multiple CPU cores.Concurrency suggests that multiple tasks have the ability to run in an overlapping manner.
Among other async features, you’ll find that you can now writeasynchronousfor
loops and comprehensions, and alsoasynchronous iterators.
Similar to normal iterators, asynchronous iterators implement theasynchronous iterator protocol, which consists of two methods:
.__aiter__()
returns an asynchronous iterator, typicallyself
..__anext__()
must return anawaitable object from a stream. It must raise aStopAsyncIteration
exception when the iterator is exhausted.Note that these methods look pretty similar to those used in normal iterators. The.__aiter__()
method replaces.__iter__()
, while.__anext__()
replaces.__next__()
. The leadinga
on their names signifies that the iterator at hand is asynchronous.
Another detail is that.__anext__()
must raiseStopAsyncIteration
instead ofStopIteration
at the end to signal that the data is over, and the iteration must end.
As an example of an asynchronous iterator, consider the following class, which producesrandom integers:
async_rand.py
importasynciofromrandomimportrandintclassAsyncIterable:def__init__(self,stop):self._stop=stopself._index=0def__aiter__(self):returnselfasyncdef__anext__(self):ifself._index>=self._stop:raiseStopAsyncIterationawaitasyncio.sleep(value:=randint(1,3))self._index+=1returnvalue
This class takes astop
value at instantiation time. This value defines the number of random integers to produce. The.__aiter__()
method returnsself
, the standard practice in iterators. There’s no need for this method to be asynchronous. So, you don’t have to use theasync def
keywords on the method’s definition.
The.__anext__()
method must be an asynchronouscoroutine, so you must use theasync def
keywords to define it. This method must return an awaitable object, which is an object that you can use in an asynchronous operation like, for example, anasync for
loop.
In this example,.__anext__()
raises aStopAsyncIteration
when the._index
attribute reaches the value in._stop
. Then, the method runs anawait
expression that computes a random integer number wrapped in a call toasyncio.sleep()
to simulate an awaitable operation. Finally, the method returns the computed random number.
Here’s how you can use this iterator in anasync for
loop:
>>>importasyncio>>>fromasync_randimportAsyncIterable>>>asyncdefmain():...asyncfornumberinAsyncIterable(4):...print(number)...>>>asyncio.run(main())3321
This code will issue a different output for you because it deals with random numbers. It’s crucial to remember that asynchronous iterators,async for
loops, and asynchronous comprehensions don’t make the iteration process parallel. They just allow the iteration to give up control to theasyncio
event loop for some other coroutine to run.
In the above example, theasyncio
event loop runs when you call theasyncio.run()
function with yourmain()
function as an argument.
You’ve learned a lot about Pythoniterators anditerables. Now you know what they are and what their main differences are. You learned how to create different types of iterators according to their specific behavior regarding input and output data.
You studiedgenerator iterators and learned how to create them in Python. Additionally, you learned how to build your own iterables using different techniques. Finally, you touched on asynchronous iterators and asynchronous loops in Python.
In this tutorial, you learned how to:
yield
statement to creategenerator iteratorsasyncio
module and theawait
andasync
keywordsWith all this knowledge, you’re now ready to leverage the power of iterators and iterables in your code. In particular, you’re able to decide when to use an iterator instead of iterable and vice versa.
Free Sample Code:Click here to download the free sample code that shows you how to use and create iterators and iterables for more efficient data processing.
Now that you have some experience with iterators and iterables in Python, you can use the questions and answers below to check your understanding and recap what you’ve learned.
These FAQs are related to the most important concepts you’ve covered in this tutorial. Click theShow/Hide toggle beside each question to reveal the answer.
An iterable is an object that can be passed to theiter()
function to get an iterator, while an iterator is an object that implements both.__iter__()
and.__next__()
methods to produce items one at a time.
You create an iterator in Python by defining a class that implements the.__iter__()
method, which returns the iterator object itself, and the.__next__()
method, which returns the next item in the sequence.
Theyield
keyword in Python is used in a function to make it a generator function. This allows it to return a generator iterator that yields values one at a time, pausing execution between yields.
You can make a custom object iterable in Python by implementing.__iter__()
in the object’s class. The method should return an iterator.
An asynchronous iterator in Python is an iterator that implements the.__aiter__()
and.__anext__()
methods, allowing you to iterate over items asynchronously usingasync for
loops.
Take the Quiz: Test your knowledge with our interactive “Iterators and Iterables in Python: Run Efficient Iterations” quiz. You’ll receive a score upon completion to help you track your learning progress:
Interactive Quiz
Iterators and Iterables in Python: Run Efficient IterationsIn this quiz, you'll test your understanding of Python's iterators and iterables. By working through this quiz, you'll revisit how to create and work with iterators and iterables, the differences between them, and review how to use generator functions.
Watch Now This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding:Efficient Iterations With Python Iterators and Iterables
🐍 Python Tricks 💌
Get a short & sweetPython Trick delivered to your inbox every couple of days. No spam ever. Unsubscribe any time. Curated by the Real Python team.
AboutLeodanis Pozo Ramos
Leodanis is an industrial engineer who loves Python and software development. He's a self-taught Python developer with 6+ years of experience. He's an avid technical writer with a growing number of articles published on Real Python and other sites.
» More about LeodanisMasterReal-World Python Skills With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
MasterReal-World Python Skills
With Unlimited Access to Real Python
Join us and get access to thousands of tutorials, hands-on video courses, and a community of expert Pythonistas:
What Do You Think?
What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.
Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students.Get tips for asking good questions andget answers to common questions in our support portal.
Keep Learning
Related Topics:intermediatepython
Recommended Video Course:Efficient Iterations With Python Iterators and Iterables
Related Tutorials:
Already have an account?Sign-In
Almost there! Complete this form and click the button below to gain instant access:
Iterators and Iterables in Python: Run Efficient Iterations (Sample Code)