Python Iterator

Python-Iterators-Feature-1.jpg

The Python iterator is a powerful tool that is used for accessing the elements in a sequence, such as lists, tuples, or strings, one item at a time. It helps in managing the memory more efficiently and allows better control during the looping process. Whether reading large files, handling real-time data, or building custom sequences, iterators help in managing these tasks very efficiently. In this blog, you will learn about the Python iterator in detail with examples.

Table of Contents:

What is Iterator in Python?

An iterator in Python is an object that helps in accessing the elements of a collection, like a list or a tuple. It allows access to one element at a time. The Python iterator also helps in keeping track of the current position of an object within the sequence, which returns elements one by one in order. For example, an iterator is similar to a TV remote, where you can press the button to move from one channel to the next, just like how the iterator moves through each item in the sequence that allows it to return the elements one by one in order.

In Python, an object is considered an iterator if it consists of the two special methods that are __iter__() and __next()___. The __iter()__ method returns the iterator object itself, the __next()__ method returns the next element that is there in the sequence. If there are no elements left, it raises an error.

Unlock Python: Power Up Your Programming Skills!
Dive into real-world coding projects and become confident in writing clean, efficient Python code.
quiz-icon

How the Iter() Function Works in Python?

The Python iter() function returns an iterator from any iterable object. An iterable is something that can be looped over, and examples of iterables are lists, tuples, strings, and dictionaries.

When you give an iterable as an argument to the iter() function, it returns an iterator object, which can be called to get each element one at a time with the next() function.

Example:

Python

Output:

How the Iter () Function Works in Python?

Explanation: Here, the iter() function converts the list containing the course names into an iterator object that helps in keeping track of the current position of the object within the list.

Examples of Python Iterators

In Python, an iterator is a special object that lets you loop through a collection, like a list or tuple, one element at a time. You can use Python's built-in iter() and next() functions to work with an iterator.

1. Accessing Elements Using next() with a Python Iterator

The next() function is used to obtain the next item from an iterator. Each time you call next(), Python will yield the next value in the sequence. If there are no items left, it raises a StopIteration error to signify completion of the iteration. This process is useful when it is important to have full control over when the next value is accessed.

Example:

Python

Output:

Accesing elements using next

Explanation: Here, the list of the names of courses is converted into an iterator, which allows us to retrieve the course one at a time using next(). When there are no items left in the list, a StopIteration exception is raised to signal the end of the sequence.

2. Creating a Python Iterator from a List

Lists are iterable in Python. You can make any list an iterator with the iter() function. In other words, once a list is an iterator, you can iterate through all its items one at a time. This approach is used often when we want to process items from a list and we do not want to create a for loop.

Example:

Python

Output:

Creating a Python Iterator from a List

Explanation: Here, the list containing the names of the programming languages is converted into an iterator and is accessed using the next() function that prints the name of each.

3. Building a Custom Iterator Class in Python

Python allows you to create your own iterator in a class. Using a class is useful when you want to return a sequence of values based on a pattern or rule. To make a class an iterator, you need to define two methods:

  • __iter__() that returns the iterator object
  • __next__() returns the next value in the sequence and raises a StopIteration exception when the sequence is complete.

Let us create a custom iterator that returns the first few square numbers.

Example:

Python

Output:

Building a Custom Iterator Class in Python

Explanation: Here, a custom iterator class SquareNumbers is used for returning the square values from 1 to the given limit using the for loop.

Get 100% Hike!

Master Most in Demand Skills Now!

StopIteration Exception in Python

In Python, once all the items have been accessed, it has no values to return. At this point, Python raises a built-in exception called StopIteration. This indicates that the iterator is finished and that there is nothing left to retrieve.

Example:

Python

Output:

StopIteration Exception in Python

Explanation: In this case, the next() method will be used within the while loop to fetch items from the list, and a StopIteration exception will be raised at the end of the iterations when the list is empty.

Working with Infinite Iterators in Python

An infinite iterator keeps generating values without stopping on its own. It never ends and continues as long as needed. These are useful when an endless list of values is required, such as repeating patterns or continuous number series. Python offers some of these in the itertools module. Since they do not stop by default, they are often used with conditions or slicing to avoid endless loops.

Example:

Python

Output:

Working with Infinite Iterators in Python

Explanation: Here, the itertools.count(1) generates an infinite sequence of numbers from 1, and islice() is used for limiting the output to the first 5 numbers.

Difference Between Python Iterators and Generators

Feature Iterators Generators
Definition An object that lets you loop through items one at a time using special methods A simple function that returns values one at a time using the yield keyword
Memory Usage Stores all items in memory which can be heavy for large data Uses less memory by generating values only when needed
Syntax Needs a class with __iter__() and __next__() methods Written as a normal function using yield
Code Simplicity More code and manual effort to track the next value Less code and automatically handles the next value
Performance Slower with large data as it loads all items Faster for large data due to lazy loading

Best Practices for Python Iterators

1. Use for Loops for Simplicity: A for loop handles iteration and exceptions. It allows you to keep your code clean and free without the need to call next() and StopIteration.

2. Use iter() to Create Iterators: The iter() function can be used to convert any list, string, or other collection into an iterator. This is helpful when you need more control when looping.

3. Handle StopIteration When Using next(): If you use next() directly, always prepare for the StopIteration exception. This stops the program from crashing when the iterator is finished.

4. Be Careful with Infinite Iterators: Infinite iterators like itertools.count() should be used along with the condition or limits. This helps in avoiding the infinite loops that are created accidentally.

5. Use Iterators to Save Memory: Iterators can process one item at a time instead of loading everything at once. This is very helpful when working with large data.

Practical Use Cases of Python Iterators

1. Reading Large Files Line by Line: Iterators can read one line at a time without the need for loading the entire file into memory. This helps handle large files that are too big to open at once.

2. Processing Real-Time Data Streams: Iterators handle one data point at a time, from sources like the sensors or the APIs. This makes them suitable for real-time applications like monitoring systems.

3. Custom Sequence Generators: Iterators can be used for creating sequences like even numbers, patterns, or custom series.. These sequences can be generated on priority without using a large amount of memory.

4. Handling Database Results with Iterators: The results of the database can be processed row by row using the iterators. This helps in avoiding the process of loading all the data at once, which is useful while processing large queries.

Start Coding in Python for Free: No Experience Needed!
Begin writing real Python code through interactive, beginner-friendly modules—completely free.
quiz-icon

Conclusion

Python iterators simplify the process of working with sequences as they access one element at a time. They help in saving memory, provide better control over data processing, and are very useful in many real-world tasks like reading files, handling database results, and working with live data. By using the iter(), next(), and custom iterator classes, Python offers the best way for handling loops and data one at a time. Following best practices makes it easier to use Python iterators effectively for writing clean and efficient code.

Take your skills to the next level by enrolling in the Python Course today and gaining hands-on experience. Also, prepare for job interviews with Python Interview Questions prepared by industry experts.

Python Iterators - FAQs

Q1. What is an iterator in Python?

An iterator is an object that returns one item at a time from a collection like a list or tuple.

Q2. How do you create an iterator in Python?

You can create an iterator using the iter() function on any iterable object.

Q3. What is the purpose of the next() function in Python?

The next() function retrieves the next item from an iterator.

Q4. What happens when an iterator has no more items?

Python raises a StopIteration exception when there are no more items to return.

Q5. When should you use iterators in real-world projects?

Use iterators when working with large data, files, or live data streams to save memory.

About the Author

Senior Consultant Analytics & Data Science, Eli Lilly and Company

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

EPGC Data Science Artificial Intelligence