Python Iterators: Understanding Iterables and Iterators in Python

← Back to Home

🔁 Python Iterators Explained (Beginner-Friendly + Pro-Level Guide)


Iterators are one of the most interesting and powerful parts of Python’s data-processing system. Whether you're looping through a list, reading lines from a file, or generating values on the fly, chances are you’re already using iterators — even if you didn’t realize it.

This guide will help you understand iterators in a simple way, while also covering advanced concepts that professional developers use daily.



✅ What Exactly Is an Iterator?

An iterator is an object in Python that returns items one at a time. It follows two special requirements:

  1. __iter__() – returns the iterator object itself.
  2. __next__() – returns the next value in the sequence.

When there are no more items left, __next__() must raise a StopIteration error to signal completion.


🔹 Example 1: Using an Iterator Manually

letters = ["a", "b", "c", "d"]

# Create iterator
it = iter(letters)

print(next(it))  # a
print(next(it))  # b
print(next(it))  # c
print(next(it))  # d

If you call next(it) again, Python raises:

StopIteration


🔍 What Happens Behind the Scenes in a For Loop?

When you run:

for ch in ["x", "y", "z"]:
    print(ch)

Python automatically performs:

  • iter() → to get the iterator
  • next() repeatedly → until StopIteration occurs

This is why loops in Python are both clean and memory-efficient.



🛠️ Creating Your Own Custom Iterator

Let’s build an iterator that returns numbers from 10 to 15 one at a time.

Example 2: Custom Iterator Class

class RangeMaker:
    def __iter__(self):
        self.start = 10
        return self

    def __next__(self):
        if self.start <= 15:
            value = self.start
            self.start += 1
            return value
        else:
            raise StopIteration

nums = RangeMaker()

for n in nums:
    print(n)

Output:

10
11
12
13
14
15


📌 Iterables vs Iterators (Important Difference)

Iterables Iterators
Objects that can return an iterator Objects that give values one at a time
List, tuple, dict, set, string Objects created using iter() or custom classes
Do NOT have __next__() Must have __next__()


🧵 Example 3: Iterating Over a String

text = "OK"
it = iter(text)

print(next(it))  # O
print(next(it))  # K


⚡ Why Iterators Are Useful

  • They process items **one at a time** → saves memory.
  • Used in loops, files, generators, ranges, etc.
  • Ideal for large datasets.
  • Form the foundation of lazy evaluation.


🚀 ADVANCED: Generator Functions (Iterator Shortcut)

Writing a complete iterator class can be long. Generators make this easier using the yield keyword.

Example 4: Generator Function

def countdown(n):
    while n > 0:
        yield n
        n -= 1

for num in countdown(5):
    print(num)

Output:

5
4
3
2
1

Generators are iterators automatically, no need to write __iter__ or __next__.



⚙️ Generator Expressions

Similar to list comprehensions but much lighter in memory.

squares = (x*x for x in range(1, 6))

for s in squares:
    print(s)


📦 itertools: Powerful Iterator Tools (Pro-Level)

The itertools module provides built-in fast iterators.

1. count() → infinite counter

from itertools import count

for n in count(3):
    if n > 8:
        break
    print(n)

2. cycle() → repeat items

from itertools import cycle

count = 0
for item in cycle(["A", "B"]):
    if count == 4:
        break
    print(item)
    count += 1

3. chain() → combine iterables

from itertools import chain

for x in chain([1, 2], ["a", "b"]):
    print(x)


📘 Real-World Use Cases of Iterators

  • Reading huge files line by line
  • Processing streams of data
  • Creating infinite sequences
  • Saving memory when working with large collections
  • Data pipelines & generators in machine learning


🎯 Conclusion

Iterators power the looping mechanism in Python and make data processing efficient. Whether you're building custom sequences, processing large files, or creating pipelines with generators, understanding iterators gives you a solid advantage as a Python developer.

Iterators are a powerful tool in Python that let you go through items one at a time. Whether you are working with lists, strings, or building custom sequences, understanding iterators helps us write clean and efficient code.




❓ FAQs

1. Are all iterables also iterators?

No. Lists, tuples, and strings are iterables, but not iterators. They return iterators when passed to iter().

2. What is StopIteration?

It is an exception raised when an iterator has no more values to return.

3. Are generators faster than normal functions?

Yes, because they produce values one at a time rather than storing them in memory.

4. Is a for loop faster than manually calling next()?

Yes. For-loops in Python are optimized internally and run faster.

5. When should I use itertools?

Use itertools when you need high-performance iteration tools or must work with large or infinite sequences.



📢 What’s Next?

If this guide helped you understand iterators, explore the next topic to level up your Python skills even further:

Practice creating your own iterators and try using generators in your own next Python script.