Python Functions creating iterators for efficient looping

In this article, we will talk about creating iterators for efficient looping in Python functions. An iterator is an object that helps you go through all items one by one, like reading pages of a book sequentially.

In Python, we can use lists, tuples, dictionaries, and sets as iterables. Let's explore Python functions that generate iterators for efficient looping and memory management.

What are Iterators?

An iterator is an object that implements the iterator protocol, consisting of __iter__() and __next__() methods. Iterators are memory-efficient because they generate values on demand rather than storing all values in memory at once.

Using Generator Functions

Generator functions use the yield keyword to pause execution and return values one at a time. This creates a memory-efficient iterator that generates values on demand.

Example

The following example generates numbers from 1 to 3 using yield ?

# Create a generator function
def gen_vals():
    n = 1
    while n <= 3:
        # Use yield to return a value and pause
        yield n
        n += 1

# Use the generator function
for v in gen_vals():
    print(v)
1
2
3

Using Generator Expressions

Generator expressions provide a concise way to create iterators using syntax similar to list comprehensions, but with parentheses instead of square brackets.

Example

Creating squares of numbers using a generator expression ?

# Creates squares of numbers using generator expression
nums = (x**2 for x in range(1, 6))

# Loop through the generator expression
for n in nums:
    print(n)
1
4
9
16
25

Using iter() Function

The iter() function creates an iterator from any iterable object. Combined with next(), it allows manual iteration through elements.

Example

Creating an iterator from a list and accessing elements manually ?

my_list = [10, 20, 30]
it = iter(my_list)

print(next(it))
print(next(it))
print(next(it))
# Calling next() again would raise StopIteration
10
20
30

Using Custom Iterator Classes

You can create custom iterators by defining a class with __iter__() and __next__() methods. This provides full control over the iteration behavior.

Example

Creating a custom iterator class that generates numbers in a range ?

# Define a custom iterator class
class MyIterator:
    def __init__(self, start, end):
        self.current = start
        self.end = end

    def __iter__(self):
        return self

    def __next__(self):
        if self.current > self.end:
            raise StopIteration
        else:
            value = self.current
            self.current += 1
            return value

# Create an instance of MyIterator
my_iter = MyIterator(1, 5)

# Loop through the custom iterator
for num in my_iter:
    print(num)
1
2
3
4
5

Using itertools Module

The itertools module provides specialized iterator functions like count(), cycle(), and repeat() for creating efficient iterators with specific patterns.

Example

Using itertools.count() to create an infinite arithmetic sequence ?

import itertools

# Create an iterator using itertools.count
counter = itertools.count(start=1, step=2)

# Loop through the iterator (first 5 values)
for _ in range(5):
    print(next(counter))
1
3
5
7
9

Comparison of Iterator Methods

Method Memory Usage Best For
Generator Functions Low Complex iteration logic
Generator Expressions Low Simple transformations
Custom Classes Low Reusable complex iterators
itertools Low Specialized patterns

Conclusion

Python iterators provide memory-efficient ways to process data sequentially. Use generator functions for complex logic, generator expressions for simple transformations, and itertools for specialized patterns. All methods create lazy evaluation, processing items only when needed.

Updated on: 2026-03-25T05:08:51+05:30

352 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements