<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Iterators on cassiobotaro</title><link>https://cassiobotaro.dev/en/tags/iterators/</link><description>Recent content in Iterators on cassiobotaro</description><generator>Hugo</generator><language>en</language><lastBuildDate>Sat, 25 Jul 2026 13:06:19 -0300</lastBuildDate><atom:link href="https://cassiobotaro.dev/en/tags/iterators/index.xml" rel="self" type="application/rss+xml"/><item><title>The recipes hidden in the itertools documentation</title><link>https://cassiobotaro.dev/en/posts/itertools-recipes/</link><pubDate>Mon, 13 Jul 2026 00:00:00 +0000</pubDate><guid>https://cassiobotaro.dev/en/posts/itertools-recipes/</guid><description>&lt;p&gt;At the end of the &lt;code&gt;itertools&lt;/code&gt; page there is a section called &lt;a href="https://docs.python.org/3/library/itertools.html#itertools-recipes" class="external-link" target="_blank" rel="noopener"&gt;Itertools Recipes&lt;/a&gt;: a collection of useful functions built by combining the module&amp;rsquo;s building blocks. Flattening a list of lists, finding the first element that passes a test, walking a sequence in windows: a lot of what you would write by hand is already solved there. The recipes do not come ready to use in &lt;code&gt;itertools&lt;/code&gt;, the idea is to copy them into your code. We have already used one of them here, &lt;code&gt;consume&lt;/code&gt;, in the article about &lt;a href="https://cassiobotaro.dev/en/posts/consuming-iterables/" &gt;consuming iterables without allocating memory&lt;/a&gt;. I picked three others that are worth knowing.&lt;/p&gt;</description></item><item><title>Consuming iterables without allocating memory</title><link>https://cassiobotaro.dev/en/posts/consuming-iterables/</link><pubDate>Wed, 17 Jun 2026 00:00:00 +0000</pubDate><guid>https://cassiobotaro.dev/en/posts/consuming-iterables/</guid><description>&lt;h2 id="the-problem"&gt;
 The problem
 &lt;a class="heading-link" href="#the-problem"&gt;
 &lt;i class="fa-solid fa-link" aria-hidden="true" title="Link to heading"&gt;&lt;/i&gt;
 &lt;span class="sr-only"&gt;Link to heading&lt;/span&gt;
 &lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;When you just want to &amp;ldquo;run&amp;rdquo; a &lt;code&gt;map&lt;/code&gt;/generator pipeline for its side effects (writing each item to a stream, for example), the reflex is a &lt;code&gt;for&lt;/code&gt; or a &lt;code&gt;list(...)&lt;/code&gt;. The &lt;code&gt;list&lt;/code&gt; allocates an entire list that gets thrown away right after, wasting memory for nothing. The &lt;code&gt;for&lt;/code&gt; solves the memory issue, but the loop runs in pure Python.&lt;/p&gt;
&lt;h2 id="the-tip"&gt;
 The tip
 &lt;a class="heading-link" href="#the-tip"&gt;
 &lt;i class="fa-solid fa-link" aria-hidden="true" title="Link to heading"&gt;&lt;/i&gt;
 &lt;span class="sr-only"&gt;Link to heading&lt;/span&gt;
 &lt;/a&gt;
&lt;/h2&gt;
&lt;p&gt;&lt;code&gt;collections.deque&lt;/code&gt; accepts a &lt;code&gt;maxlen&lt;/code&gt; parameter. With &lt;code&gt;maxlen=0&lt;/code&gt;, it pulls each element and discards it immediately, never growing: that is the &lt;code&gt;itertools&lt;/code&gt; &lt;a href="https://docs.python.org/3/library/itertools.html#itertools-recipes" class="external-link" target="_blank" rel="noopener"&gt;&lt;code&gt;consume&lt;/code&gt;&lt;/a&gt; recipe. Instead of materializing a list only to throw it away, you drain the pipeline in constant memory.&lt;/p&gt;</description></item></channel></rss>