How to efficiently use Iterators in Python

With Code Implementation

Rohit Chaturvedi
5 min readMay 17, 2023

Let’s imagine you have a big box full of toys. You want to play with each toy one by one, but you don’t want to grab all the toys at once because it would be too much to handle. That’s where an iterator comes in.

An iterator is like a special pointer that helps you go through the toys in the box, one toy at a time. It knows which toy you played with last and helps you move on to the next toy without getting confused.

So, when you want to play with a toy, you ask the iterator to give you the next toy. The iterator points to the first toy and gives it to you. Then, when you’re done playing with that toy, you tell the iterator to give you the next one, and it gives you the second toy. This goes on until you’ve played with all the toys in the box.

The iterator keeps track of the toys for you so you don’t have to remember which one you played with last. It’s like having a special helper to make sure you don’t miss any toys and that you play with each one in order.

An iterator is a concept in programming that helps you go through a collection of items or elements one by one. It’s like having a special tool that allows you to access each item in a sequence, such as a list or a set, without needing to know all the items beforehand.

To understand iterators, let’s imagine you have a big box filled with different toys. Instead of taking out all the toys at once, an iterator lets you pick one toy at a time. It keeps track of which toy you currently have and helps you move to the next one when you’re ready.

In programming, an iterator is an object that works similarly. It keeps track of the current position within a collection and provides a way to access the next item in the sequence. You can think of it as a magical pointer that points to the current item and allows you to move to the next item whenever you want.

Using an iterator, you can loop or iterate over a collection, like going through all the toys in the box. Each time you ask the iterator for the next item, it gives it to you, and you can do something with that item. When you reach the end of the collection, the iterator lets you know that there are no more items left.

Iterators are helpful because they allow you to work with large collections of data efficiently. You can process or use the items one by one without needing to load all the data into memory at once. It makes working with big collections easier and more manageable.

So, in simple terms, an iterator is a tool that helps you go through a collection of items one by one, like going through toys in a box, without needing to grab all the toys at once.

Iterators are used in various situations when you want to work with collections of data or perform certain operations on them. Here are some scenarios where iterators are commonly used and the reasons behind their usage:

  1. Looping over large collections: If you have a collection of data with a large number of items, using an iterator allows you to loop over the items one at a time without loading the entire collection into memory. This saves memory resources and improves performance, especially when dealing with large datasets.
  2. Lazy evaluation: Iterators enable lazy evaluation, which means the next item is computed or fetched only when it is needed. This can be useful when working with computationally expensive or memory-intensive operations. Lazy evaluation allows you to defer the computation or retrieval of items until they are actually needed, optimizing resource usage.
  3. Custom iteration behavior: Iterators provide a way to define custom iteration behavior for your own classes or objects. By implementing the iterator protocol in Python (using the __iter__() and __next__() methods), you can make your objects iterable and define how they should be iterated over. This gives you control over the order, filtering, or transformations applied during iteration.
  4. Working with external data sources: Iterators are useful when working with external data sources that provide data one item at a time. For example, reading data from a file or fetching data from a network API. By using an iterator, you can process the data as it becomes available, without having to load the entire data source into memory.
  5. Memory efficiency: Iterators can be memory-efficient because they allow you to process items one at a time, without needing to store the entire collection in memory. This is especially important when dealing with large datasets or when memory resources are limited.
  6. Infinite or dynamically generated sequences: Iterators are perfect for handling infinite sequences or dynamically generated sequences of data. For example, generating prime numbers, iterating over a range of dates, or generating random values on the fly. Iterators allow you to work with these sequences without needing to generate or store the entire sequence upfront.

In summary, iterators are used when you want to iterate over large collections of data, work with external data sources, customize iteration behavior, optimize memory usage, or handle infinite or dynamically generated sequences. They provide a flexible and efficient way to process data one item at a time, making them essential tools in many programming scenarios.

Example of how to implement iterators using Python code.

In the code above, we define a class called NumberIterator that represents our iterator. It takes a list of numbers as input during initialization.

The __iter__() method is implemented to make the iterator itself iterable. It simply returns the instance of the iterator.

The __next__() method is where the iteration logic resides. It returns the next number in the list and updates the index to move to the next number. If we reach the end of the list, it raises the StopIteration exception to indicate that there are no more items.

Now, let’s see how we can use this iterator:

In the code above, we create an instance of NumberIterator by passing in a list of numbers. We can then use this iterator in a for loop to iterate over the numbers. Each iteration, the __next__() method is called to retrieve the next number from the iterator.

The output of the above code will be:

That’s how you can implement an iterator in Python! You can create custom iterators for different types of collections or objects by defining the __iter__() and __next__() methods appropriately.

--

--