Hackforge Academy

Category: python

What Is an Iterable?

Published on 27 Oct 2026

Explanation

An iterable can provide an iterator. Lists, tuples, strings, dictionaries, sets, and ranges are common examples.

Code:

values = [10, 20, 30]
print(iter(values))
for value in values: print(value)

Explanation

An iterator produces values through next() and normally becomes exhausted after all values are consumed.

Code:

it = iter([10, 20, 30])
print(next(it), next(it), next(it))
print(next(it, 'Done'))

Explanation

iter() obtains an iterator from an iterable, which can then be consumed one value at a time.

Code:

colors = ['red', 'green', 'blue']
it = iter(colors)
print(next(it))
for color in it: print(color)

Explanation

A list can provide fresh iterators. An exhausted iterator does not start over automatically.

Code:

values = [1, 2, 3]
print(list(iter(values)))
it = iter(values)
print(list(it))
print(list(it))

Explanation

A reusable custom iterable returns a fresh iterator whenever __iter__() is called.

Code:

class NumberRange:
    def __init__(self, start, stop): self.start, self.stop = start, stop
    def __iter__(self): return iter(range(self.start, self.stop))
nums = NumberRange(1, 4)
print(list(nums), list(nums))

πŸš€ Learn Spring Boot with real-world projects

πŸ’‘ Build REST APIs step by step

🧠 Improve backend development skills

🎯 Get career-ready practical training

Join Our Free WhatsApp Community

Direct access to niche-specific mentors and peers on WhatsApp.

🐍

Python Community

Discuss Django, FastAPI, AI integration, and automation scripts with 15k+ developers.

Join Python Community
βš›οΈ

React Community

Master Next.js, Framer Motion, and State Management. Share your latest UI components.

Join React Community
β˜•

Java Community

Deep dives into Spring Boot, Microservices architecture, and high-performance backend ops.

Join Java Community