One a Day One Liners with Python — Week 7
The Itertools Module
This week we’re going to explore the versatility and utility of the itertools
module in Python. As always, feel free to leave a comment or suggestion, or better yet, check out the repository on Github and contribute to the One Liner cause!
Feb 18, 2023
Easily generate all r
permutations of a given list of items 🪄
from csv import reader
from itertools import chain, permutations
with open('./data/csv/songs.csv') as f:
r = 2
perms = permutations(
chain.from_iterable([row[0].split(' ') for row in reader(f)]), r
)
Discussion
Generating a list of permutations couldn’t get much easier than is made possible by the itertools
module. In this One Liner, all of the artist names in songs.csv are split into their individual parts, concatenated and then fed into permutations
. This generates a rather entertaining list of various artists names all mixed together. So what else was I to do except feed some of these names to Dall-E?







Feb 17, 2023
Filter out rows of data based on a character range of a given column 🥡
from itertools import dropwhile, takewhile
from string import ascii_lowercase as chars
start, end = 'd', 'm'
filtered = list(
dropwhile(lambda row: chars.index(row[0][0].lower()) < chars.index(start),
takewhile(lambda row: chars.index(row[0][0].lower()) <= chars.index(end), data)
)
)
Discussion
This little beast returns a range of rows from a given dataset where the first column starts with a character between start
and end
(inclusive).
From the inside out, first we take all the rows where the first char comes before end
and then we drop all rows where the first char comes before start
.
The first argument to both dropwhile
and takewhile
are predicate functions which provide the rules for determining whether or not to take or drop a given row.
Feb 16, 2023
Group items in tabular data by column 💿
from csv import reader
from itertools import groupby
with open('./data/csv/songs.csv') as f:
groups = groupby(
sorted(reader(f), key=lambda row: row[3]), lambda row: row[3]
)
Feb 15, 2023
Unleash chaos into Universe by computing values of the Logistic Map 💥
from itertools import accumulate, repeat
lm = lambda r: [
round(x, 2) for x in accumulate(repeat(0.1, 100), lambda x, _: r*x*(1-x))
]
Discussion
The “Logistic Map” is a classic example of how complex behavior can arise from small changes in non-linear systems. This One Liner was inspired by the official itertools
docs, where there is a similar example. I turned it into a lambda function so that we can pass in the value of r
.
In the plot above, we see the value of r
moving from 2 to 4 in increments of 0.001. The code for plotting is omitted from the snippet above, but it’s available in the repo.
Resources
Feb 14, 2023
Calculate the moving average for stock data 📈

from itertools import accumulate
days = 100
smoothing = 2
ema = list(
accumulate(
data,
lambda a, b: (b * (smoothing / (1+days)) + (a * (1-(smoothing / (1+days)))))
)
)
Discussion
Once again, accumulate
comes in quite handy. The parameter a
in the lambda function is the accumulated value and b
is the current item from the iterable. I’ve ommitted the data loading and plotting portions from the snippet above, but it can be found in the repo.
Feb 13, 2023
Run a batch process using iter
and islice
🍪
from itertools import islice
n = 6
it = iter([i for i in range(100)])
while batch := tuple(islice(it, n)): process(batch)
Discussion
This One Liner is adapted from an example in the official itertools
docs.
A couple of interesting things are happening here. Firstly, we have the walrus operator which is a nice way to assign a value in place. Then we have the islice
method from the itertools
package, which works much like the regular slice operator for iterable types.
Once islice
encounters a StopIteration
exception, it will make one last iterable of any remaining elements in the input and return them. This means that n
, which is the number of items for each batch does not need to divide the collection evenly.
Feb 12, 2023
Compute the powers of 2 up to N with starmap
✨ 🗺️
from itertools import starmap
from math import pow
N = 16
p2 = [x for x in starmap(pow, [(2, i) for i in range(0, N)])]
Discussion
starmap
is like map
, but specifically designed to unpack iterables of the same length as the expected number of parameters for the supplied function. In this case the pow
function is expecting two parameters, the base and the exponent.
More content at PlainEnglish.io.
Sign up for our free weekly newsletter. Follow us on Twitter, LinkedIn, YouTube, and Discord.
Interested in scaling your software startup? Check out Circuit.