Iterators are objects that can be iterated upon. They serve as a common feature of the Python programming language, neatly tucked away for looping and list comprehensions. Any object that can derive an iterator is known as an iterable.
There is a lot of work that goes into constructing an iterator. For instance, the implementation of each iterator object must consist of an __iter__() and __next__() method. In addition to the prerequisite above, the implementation must also have a way to track the object's internal state and raise a StopIteration exception once no more values can be returned. These rules are known as the iterator protocol.
Implementing your own iterator is a drawn-out process, and it is only sometimes necessary. A simpler alternative is to use a generator object. Generators are a special type of function that use the yield keyword to return an iterator that may be iterated over, one value at a time.
The ability to discern the appropriate scenarios to implement an iterator or use a generator will improve your skills as a Python programmer. In the remainder of this tutorial, we will emphasize the distinctions between the two objects, which will help you decide the best one to use for various situations.
Glossary
|
Term
|
Definition
|
|
Iterable
|
A Python object which can be looped over or iterated over in a loop. Examples of iterables include lists, sets, tuples, dictionaries, strings, etc.
|
|
Iterator
|
An iterator is an object that can be iterated upon. Thus, iterators contain a countable number of values.
|
|
Generator
|
A special type of function which does not return a single value: it returns an iterator object with a sequence of values.
|
|
Lazy Evaluation
|
An evaluation strategy whereby certain objects are only produced when required. Consequently, certain developer circles also refer to lazy evaluation as “call-by-need.”
|
|
Iterator Protocol
|
A set of rules that must be followed to define an iterator in Python.
|
|
next()
|
A built-in function used to return the next item in an iterator.
|
|
iter()
|
A built-in function used to convert an iterable to an iterator.
|
|
yield()
|
A python keyword similar to the return keyword, except yield returns a generator object instead of a value.
|
Python Iterators & Iterables
Iterables are objects capable of returning their members one at a time – they can be iterated over. Popular built-in Python data structures such as lists, tuples, and sets qualify as iterables. Other data structures like strings and dictionaries are also considered iterables: a string can produce iteration of its characters, and the keys of a dictionary can be iterated upon. As a rule of thumb, consider any object that can be iterated over in a for-loop as an iterable.
Exploring Python iterables with examples
Given the definitions, we may conclude that all iterators are also iterable. However, every iterable is not necessarily an iterator. An iterable produces an iterator only once it is iterated on.
To demonstrate this functionality, we will instantiate a list, which is an iterable, and produce an iterator by calling the iter() built-in function on the list.
list_instance = [1, 2, 3, 4]
print(iter(list_instance))
"""
<list_iterator object at 0x7fd946309e90>
"""
Although the list by itself is not an iterator, calling the iter() function converts it to an iterator and returns the iterator object.
To demonstrate that not all iterables are iterators, we will instantiate the same list object and attempt to call the next() function, which is used to return the next item in an iterator.
list_instance = [1, 2, 3, 4]
print(next(list_instance
Iterators are objects that can be iterated upon. They serve as a common feature of the Python programming language, neatly tucked away for looping and list comprehensions. Any object that can derive an iterator is known as an iterable.
There is a lot of work that goes into constructing an iterator. For instance, the implementation of each iterator object must consist of an
__iter__()and__next__()method. In addition to the prerequisite above, the implementation must also have a way to track the object's internal state and raise aStopIterationexception once no more values can be returned. These rules are known as the iterator protocol.Implementing your own iterator is a drawn-out process, and it is only sometimes necessary. A simpler alternative is to use a generator object. Generators are a special type of function that use the
yieldkeyword to return an iterator that may be iterated over, one value at a time.The ability to discern the appropriate scenarios to implement an iterator or use a generator will improve your skills as a Python programmer. In the remainder of this tutorial, we will emphasize the distinctions between the two objects, which will help you decide the best one to use for various situations.
Glossary
Term
Definition
Iterable
A Python object which can be looped over or iterated over in a loop. Examples of iterables include lists, sets, tuples, dictionaries, strings, etc.
Iterator
An iterator is an object that can be iterated upon. Thus, iterators contain a countable number of values.
Generator
A special type of function which does not return a single value: it returns an iterator object with a sequence of values.
Lazy Evaluation
An evaluation strategy whereby certain objects are only produced when required. Consequently, certain developer circles also refer to lazy evaluation as “call-by-need.”
Iterator Protocol
A set of rules that must be followed to define an iterator in Python.
next()
A built-in function used to return the next item in an iterator.
iter()
A built-in function used to convert an iterable to an iterator.
yield()
A python keyword similar to the return keyword, except
yieldreturns a generator object instead of a value.Python Iterators & Iterables
Iterables are objects capable of returning their members one at a time – they can be iterated over. Popular built-in Python data structures such as lists, tuples, and sets qualify as iterables. Other data structures like strings and dictionaries are also considered iterables: a string can produce iteration of its characters, and the keys of a dictionary can be iterated upon. As a rule of thumb, consider any object that can be iterated over in a for-loop as an iterable.
Exploring Python iterables with examples
Given the definitions, we may conclude that all iterators are also iterable. However, every iterable is not necessarily an iterator. An iterable produces an iterator only once it is iterated on.
To demonstrate this functionality, we will instantiate a list, which is an iterable, and produce an iterator by calling the
iter()built-in function on the list.Although the list by itself is not an iterator, calling the
iter()function converts it to an iterator and returns the iterator object.To demonstrate that not all iterables are iterators, we will instantiate the same list object and attempt to call the
next()function, which is used to return the next item in an iterator.