Python Practice › Iterator & Generator Programs › Program 2
A for loop by hand
Write a Python program that rebuilds a for loop using while, iter() and next().
Output
10 20 30
Approach
- Call
next()untilStopIteration.
Solution
items = [10, 20, 30]it = iter(items)while True: try: value = next(it) except StopIteration: break print(value)Output
10 20 30
Learn the concept
New to this topic? Read the lesson, then come back.