Iterator Functions
Iterate Efficiently with Python’s Built-in Functions
Hi everyone, Thijmen here, and in this article, I will demonstrate several built-in Python iterator functions to process your iterables efficiently without a for-loop.
Overview
Here is what to expect from this article:
- A brief explanation of iterables
- Why iterator functions are useful
- A demonstration of 10 built-in iterator functions (and 1 bonus)
The functions included in this article are the following:
- Function 1:
all
- Function 2:
any
- Funciton 3:
enumerate
- Function 4:
filter
- Function 5:
map
- Function 6:
min
- Function 7:
max
- Function 8:
reversed
- Function 9:
sum
- Function 10:
zip
- Bonus:
collections.Counter
A Brief Explanation of Iterables
An iterable is any Python object that is capable of returning its members one at a time, permitting it to be iterated over in a loop.
There are sequential iterables that arrange items in a specific order, such as lists, tuples, string and dictionaries.
There are also non-sequential collections that are iterable. For example, a set is an iterable, despite lacking any specific order.
Iterables are fundamental to Python and any other programming language. Knowing how to efficiently leverage them will have an appreciable impact on the quality of your code.
Why Iterator Functions Are Useful
In general, iterables can be easily processed by creating a for-loop that allows for successively handling each item that is part of the iterable.
However, you might have found yourself frequently programming loops that are relatively similar to one another.
For that reason there are plenty of great built-in Python functions that help you process your iterables without reinventing the wheel.
They are fast, memory-efficient, and when used properly make your code more concise and readable.
💡 Python’s built-in functions are actually written in C, and are therefore very efficient for Python’s standards.
In the following sections I will explain 10 built-in iterator functions, and when useful provide a simplified for-loop equivalent to help you understand how the function works.
Function 1: all
all()
takes an iterable as argument, and returns True
if all elements of the iterable evaluate to True
. Returns False
otherwise.
Note that if the iterable is empty, all()
returns True
as well.
Function 2: any
any()
takes an iterable as argument, and returns True
if any element of the iterable evaluates to True
. Returns False
otherwise.
Contrary to all()
, False
is returned if the iterable is empty.
Function 3: enumerate
enumerate()
takes an iterable as argument, and returns a generator that assigns an incrementing number to each sequential item in the iterable.
Incrementing starts at 0 by default, but a different start value can be provided by the optional start
parameter.
Function 4: filter
As the name suggests, filter()
is used to create a subset of an iterable.
The function takes an iterable and a conditional function as parameters, and applies this function to each item in the iterable. Only items for which the conditional function returns True
are retained and stored in the subset.
Function 5: map
map()
is one of my favorite built-in Python functions, and is used to transform the items of an iterable.
It takes an iterable and a transformer function as parameters and applies this function to every item of the iterable, yielding the results (i.e. return a generator).
Function 6 & 7: min & max
As their names suggest, min()
and max()
return respectively the minimum and maximum value of an iterable. The usual behavior of Python’s less than or greater than operator (i.e. <
or >
) is applied to determine the output of the function.
Note that if you are confused by the output of the min()
and max()
functions on a list of strings (e.g. the image below), understand that strings are evaluated based on their alphabetical order.
Function 8: reversed
reversed()
is simple yet useful. It reverses the provided iterable and yields the results (i.e. return a generator).
Note that whenever a dictionary is provided, a reverse iterator of the dictionary’s keys is returned, rather than a reversed dictionary.
Function 9: sum
Not much to say here! sum()
adds up all elements in an iterable and returns the result.
Note that strings are not supported, even though strings support the +
operator (see the error on the image below).
💡 If you want to “sum” a collection of strings, use
string.join()
instead.
Function 10: zip
zip()
is yet another favorite of mine. It allows to iterate over several iterables in parallel, producing a generator with tuples that include one item of each iterable.
To visualize zip()
's output, imagine a zipper that you’d find on your clothing: it brings two different parts together (or in this case: two or more!).
If the provided iterables differ in length, the shortest iterable determines the length of the output.
Bonus: collections.Counter
There is one non-built-in function that I just had to include in this article: the Counter()
function from Python’s collections
module.
As the name suggests, Counter()
counts the elements of an iterable and returns a collection where the elements are stored as dictionary keys and their counts are stored as dictionary values.
💡 Python includes the
collections
library by default, so no download is required.
To useCounter()
, we first have to import the class from Python’s collections
library. We can then simply pass an iterable to its constructor, and Counter()
does all the hard work for us!
Note that elements with an equal occurrence are sorted in the order first encountered.
If you are merely interested in the N most frequent elements of an iterable, Counter()
provides a convenient method called most_common(n)
. It returns a list of the N most common elements and their counts, sorted from most to least common.
That’s it for this article! I hope you found it useful, and remember:
Using built-ins everyday keeps redundant lines away
If you learned something new from this article, please consider subscribing to my YouTube channel or following me on Medium. Thanks! 🙂
This article and the corresponding video are part of my Python Snippets series, where I cover a variety of topics around Python programming in a byte-sized format.