# Itertools for CircuitPython

## Overview

![](https://cdn-learn.adafruit.com/assets/assets/000/073/601/medium800/circuitpython_refresh-round-symbol.png?1553520664)

## Iteration

What is iteration? Doing the same thing repeatedly, generally with a different, but related, piece of data. Programmers use iteration all the time, often even the ones who are using [recursion](https://learn.adafruit.com/scheme-in-circuitpython/lisp).

Every time you are using `for` to process items in a list, you're using iteration. Every time you use a list comprehension, you're using iteration.

It's so common that Python has some builtin constructs to make it easier: iterators, iterables, and generators. These were covered in [another guide](https://learn.adafruit.com/circuitpython-101-list-and-things-iterators-generators/).&nbsp; As glimpsed in that guide, Python has a module that provides a wealth of tools for working with these: **itertools**.

In that guide we briefly looked at&nbsp;the small subset of itertools that has been [ported to MicroPython](https://github.com/micropython/micropython-lib/tree/master/itertools).

This guide describes a fuller port that has been done to CircuitPython. Additionally, the [itertools recipes functions](https://docs.python.org/3.6/library/itertools.html#itertools-recipes) have been largely made available as well.

Two modules are available as helpers in the CircuitPython Bundle:

**adafruit\_itertools** - a port of the majority of Python's itertools functions.

**adafruit\_itertools\_extras** - a port of many of the itertools recipes functions. These are very useful function that are built on top of the core itertools module.

[Go to GitHub to get the latest CircuitPython library bundle](https://github.com/adafruit/Adafruit_CircuitPython_Bundle/releases/)
## Why itertools?

Similar to how the math module provides functions that operate on numbers, itertools gives you functions that operate on iterators. This lets you code in a more _functional_\* or _dataflow-centric_ way, also sometimes known as _stream-based_ programming..

The Python3/CPython version is implemented in C. The CircuitPython implementation is a pure Python implementation so it's performance isn't nearly as good, but should be adequate in most, if not all, situations. It has the additional benefit of being readily studied and understood.

One of the major advantage of using iterators is that they can be used to compute one value at a time rather than all the values at once. This has several implications:

1. Computing time is spread out as required rather than all at once to populate (for example) a list of values.
2. Since elements are computed as needed, memory to store the entire list is not required. This means you can process more data with less memory. This is very handle in a memory-limited environment.
3. Since time and space is only required for one element at a time, we can deal with sequences of any length, even infinite, without any additional requirements.

<sup>*Functional as in <em>functional programming</em>, not as in <em>it works</em>.</sup>

<sup>Header image icon made by <a href="https://www.flaticon.com/authors/designerz-base" target="_blank">Designerz Base</a> from <a title="Flaticon" href="http://www.flaticon.com/">www.flaticon.com</a></sup>

- [Next Page](https://learn.adafruit.com/itertools-and-functools-for-circuitpython/itertools.md)

## Related Guides

- [LoRa Signal Bridge with the Feather RP2040 RFM ](https://learn.adafruit.com/lora-signal-bridge-with-the-feather-rp2040-rfm.md)
- [Adafruit MacroPad RP2040](https://learn.adafruit.com/adafruit-macropad-rp2040.md)
- [Soundboard Speaker for Bikes & Scooters](https://learn.adafruit.com/soundboard-speaker-for-bikes-scooters.md)
- [Adafruit DS1841 I2C Logarithmic Resistor](https://learn.adafruit.com/adafruit-ds1841-i2c-logarithmic-resistor.md)
- [Haunted Air Blaster](https://learn.adafruit.com/automated-air-blaster.md)
- [Adafruit SGP41 Multi-Pixel Gas Sensor Breakout](https://learn.adafruit.com/adafruit-sgp41-multi-pixel-gas-sensor-breakout.md)
- [Blinka Says Tabletop Arcade Game](https://learn.adafruit.com/blinka-says-tabletop-arcade-game.md)
- [CAN Bus with CircuitPython: Using the canio module](https://learn.adafruit.com/using-canio-circuitpython.md)
- [Adafruit STSPIN220 Stepper Motor Driver Breakout Board](https://learn.adafruit.com/adafruit-stspin220-stepper-motor-driver-breakout-board.md)
- [Touch Tone Phone Dial-a-Song](https://learn.adafruit.com/touch-tone-phone-dial-a-song.md)
- [Adafruit S-35710 Low-Power Wake Up Timer Breakout](https://learn.adafruit.com/adafruit-s-35710-low-power-wake-up-timer-breakout.md)
- [Circuit Playground TFT Gizmo Dreidel](https://learn.adafruit.com/circuit-playground-tft-gizmo-dreidel.md)
- [Scan QR Codes with CircuitPython](https://learn.adafruit.com/scan-qr-codes-with-circuitpython.md)
- [Light Up Prop with Prop-Maker](https://learn.adafruit.com/prop-maker-light-wand.md)
- [NeoPixel Ring Lamp](https://learn.adafruit.com/neopixel-ring-lamp.md)
