Skip to content
Cover image for: Understanding Python Iterators & Generators
#pythonBeginner

Understanding Python Iterators & Generators

May 7, 2026
Updated May 14, 2026
30 min read

AI Insights

Powered by GPT-4o-mini

Verified Context: understanding-python-iterators-generators

Master iteration, iterators, and generators in Python from the ground up. Understand what happens internally when Python executes a for loop, build custom iterable classes by implementing __iter__ and __next__ following the iterator protocol, create memory-efficient generator functions using yield, leverage the itertools module (chain, cycle, groupby, islice, product) for powerful iterator combinations, and apply lazy evaluation to process large datasets without exhausting memory.

Quick Summary

Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.

What is an Iteration

Iteration is a general term for taking each item of something, one after another. Any time you use a loop, explicit or implicit, to go over a group of items, that is iteration.

Explanation

  • Creates a list called 'num' containing integers 1, 2, and 3
  • Uses a for loop to iterate through each element in the list
  • Calls the print() function to display each individual number on a separate line
  • The loop processes one item at a time, printing 1, then 2, then 3
  • Expected output shows each number vertically printed to console
python

Output

text

What is Iterator

An Iterator is an object that allows the programmer to traverse through a sequence of data without having to store data in the memory

How Do Iterators Optimize Memory Usage in Python?

Explanation

  • Creates a list comprehension that generates integers from 1 to 9 and stores them in variable L
  • Iterates through each element in L and prints twice its value (2, 4, 6, 8, 10, 12, 14, 16, 18)
  • Uses sys.getsizeof() to measure memory usage of the list L in units of 64-byte blocks
  • Creates a range object x containing integers from 1 to 99999 without storing all values in memory
  • Prints memory usage of the range object x, which will be much smaller than the list due to lazy evaluation
python

Output

text

What is Iterable

Iterable is an object, which one can It generates an I### How Does the iter() Function Convert a List into an Iterator in Python?assed to iter() method.

Understanding Python's iterable and iterator types through type checking

Explanation

  • The code demonstrates the difference between a list object and its iterator counterpart by using the type() function to inspect their respective types
  • When you call type() on a regular list L, it returns list, showing the object is of list type
  • Calling type() on iter(L) returns a different type representing the iterator object, demonstrating how iter() transforms an iterable into an iterator
  • This illustrates Python's iterator protocol where iter() creates an iterator from any iterable object
  • The distinction is important for understanding how for loops and other iteration mechanisms work internally in Python
python

Output

text

Point to remember

  • Every Iterator is also and Iterable
  • Not all Iterables

Trick

  • Ever### Why Can't Integers Be Iterated Over in Python? an iter function
  • Every Iterator has both iter function as well as a next function

Understanding variable assignment and method exploration in Python

Explanation

  • The variable a is assigned the integer value 2.
  • The line a retrieves the value of a, which is 2, but does not display it in a typical script context.
  • The commented-out loop for i in a: is invalid because a is an integer and cannot be iterated over.
  • The dir(a) function call lists the attributes and methods associated with the integer object a, providing insight into its capabilities.
python

Output

text

This code snippet demonstrates how to access the attributes and methods of a Python dictionary.

Explanation

  • The variable T is defined as a dictionary with two key-value pairs: 1 mapped to 2 and 3 mapped to 4.
  • The dir() function is called with T as an argument, which returns a list of the attributes and methods associated with the dictionary object.
  • The comment indicates that the Iter method is available, suggesting that the dictionary can be iterated over.
python

Output

text

Example 6

Explanation

  • The code creates a list L with elements [1,2,3] and then converts it into an iterator using the iter() function
  • The iter() function takes an iterable (like a list) and returns an iterator object that can be used to traverse through the elements one by one
  • After conversion, iter_L becomes an iterator object that maintains its own state and can be consumed sequentially
  • The original list L remains unchanged and still contains [1,2,3], but now there's also an iterator object that can be used for iteration
  • This demonstrates the difference between iterables (like lists) and iterators in Python - iterables can be converted to iterat()
python

Understanding how for loop works

This code iterates through a list of numbers and prints each one.

Explanation

  • A list named num is created containing three integers: 1, 2, and 3.
  • A for loop is used to iterate over each element in the num list.
  • The variable i takes on the value of each element in the list during each iteration.
  • The print(i) statement outputs the current value of i to the console.
  • As a result, the numbers 1, 2, and 3 are printed on separate lines.
python

Output

text

Understanding how to use iterators to traverse a list in Python

Explanation

  • The code initializes a list called num containing three integers: 1, 2, and 3.
  • It creates an iterator object iter_num from the list using the iter() function, which allows for sequential access to the list elements.
  • The next() function is called twice on the iterator iter_num, which retrieves the next item in the iteration each time it is called.
  • The first call to next(iter_num) retrieves the first element (1), and the second call retrieves the second element (2), demonstrating how to step through the iterator.
python

Output

text

Making our own for loop

This code defines a custom for loop to iterate through any iterable object in Python.

Explanation

  • The function my_for_loop takes an iterable as an argument.
  • An iterator is created from the iterable using the iter() function.
  • A while True loop is used to continuously fetch the next item from the iterator.
  • The next() function retrieves the next item, and if there are no more items, a StopIteration exception is raised.
  • The loop breaks when the StopIteration exception is caught, effectively ending the iteration.
python

This code demonstrates the use of a custom function to iterate over various data structures in Python.

Explanation

  • The code initializes different data structures: a list a, a range object b, a tuple c, a set d, and a dictionary e.
  • It calls a function my_for_loop() for each of these data structures, suggesting that the function is designed to handle multiple iterable types.
  • The output of each function call is printed, indicating that the function likely processes or transforms the elements of the iterables in some way.
  • The use of diverse data types showcases Python's flexibility in handling different collections within a single function.
  • The snippet highlights the importance of understanding how to create functions that can operate on various iterable types in Python.
python

Output

text

A confusing point

Understanding the creation and addressing of iterator objects in Python

Explanation

  • The code initializes a list num containing three integers: 1, 2, and 3.
  • An iterator object iter_obj is created from the list using the iter() function.
  • The id() function is used to print the memory address of the first iterator object.
  • A second iterator object iter_obj2 is created from the first iterator, demonstrating that iterators can be nested.
  • The memory address of the second iterator is printed, showing that it is a different object from the first iterator.
python

Output

text

Let's create our own range() function

Custom iterable class implementation for generating a range of numbers in Python

Explanation

  • Defines a class my_range that initializes with a start and end value.
  • Implements the __iter__ method to return an iterator object, allowing the class to be used in a for-loop or other iterable contexts.
  • The iterator is expected to be defined in a separate class my_range_iterator, which is not shown in this snippet.
  • This custom range class can be used to create a user-defined range functionality similar to Python's built-in range().
python

Custom iterator class for iterating over a specified range of values

Explanation

  • Defines a class my_range_iterator that implements the iterator protocol in Python.
  • The __init__ method initializes the iterator with an iterable object containing start and end attributes.
  • The __iter__ method returns the iterator object itself, allowing it to be used in a loop.
  • The __next__ method checks if the current value has reached the end; if so, it raises StopIteration to signal the end of iteration.
  • It increments the start value after returning the current value, enabling sequential access through the range.
python

This code initializes a variable with a range of numbers from 1 to 10.

Explanation

  • The function my_range is called with arguments 1 and 11, indicating the start and end of the range.
  • The variable x will store the result of this function, which is expected to be a sequence of numbers.
  • The range generated includes the starting number (1) but excludes the ending number (11), resulting in numbers 1 through 10.
  • This snippet is useful for creating a list or iterable of numbers for loops or other operations in Python.
python

This code iterates through a collection and prints each element sequentially.

Explanation

  • The for loop iterates over each item in the iterable x.
  • The variable i represents the current item in the iteration.
  • The print(i) statement outputs the value of i to the console.
  • This code is useful for displaying elements of lists, tuples, or any iterable object.
  • Ensure that x is defined and is an iterable type to avoid runtime errors.
python

Output

text

Determine the data type of a variable in Python using the type() function

Explanation

  • The type() function is a built-in Python function that returns the type of an object.
  • It can be used to check the data type of any variable, such as integers, strings, lists, etc.
  • The output of type(x) is a type object that indicates the specific class of the variable x.
  • This function is useful for debugging and ensuring that variables are of the expected type before performing operations on them.
python

Output

text

This code snippet demonstrates the invocation of a function named 'x'.

Explanation

  • The code calls a function defined elsewhere in the program.
  • The function 'x' is executed immediately when this line is reached.
  • If 'x' requires parameters, they should be provided within the parentheses.
  • The behavior and output of the function depend on its internal implementation.
python

Output

text

Frequently Asked Questions

Iteration is a general term for taking each item of something, one after another, using a loop to go over a group of items.
An iterator is an object that allows the programmer to traverse through a sequence of data without having to store data in memory.
Iterators optimize memory usage by using lazy evaluation, as demonstrated by the range object, which consumes less memory than a list by not storing all values in memory.
The iter function transforms an iterable into an iterator, as shown by the type function returning a different type for iter(L) compared to L, illustrating Python's iterator protocol.
Integers cannot be iterated over because they are not iterable objects, lacking both the iter and next functions necessary for iteration.

How was this tutorial?

Understanding Python Iterators & Generators | Madhu Dadi