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))