Iterables and iterators

Iterables and iterators

Python leans heavily on iteration: a simple, efficient way to process data one element at a time.
That’s why so many everyday objects—like list, dict, str, and files iterable.

This tutorial explains:

  • what an iterable is, and how it differs from an iterator
  • how for loops work under the hood (iter() and next())
  • why iterators are essential for working with large datasets efficiently
  • common pitfalls (like “why did my iterator become empty?”) and how to avoid them

You can run the code snippets and solve the exercises in your favorite code editor, but the tutorial is fully usable online, so you don’t need to install Python to follow it.

If you work with data (tables, logs, VCF/FASTQ files, streams…), understanding iterables and iterators will help you write code that is cleaner and more memory-efficient.

Contents

  1. Iterables and iterators
    Core concepts, the iteration protocol, and how for really works.

  2. Generators
    Creating iterators with yield, infinite sequences, and streaming patterns.

  3. Exercises
    Hands-on practice to reinforce the concepts and spot common mistakes.