A for loop by hand

EasyIterator & Generator Programs · 2 of 20

Write a Python program that rebuilds a for loop using while, iter() and next().

Advertisement
Output
10
20
30

Approach

  1. Call next() until StopIteration.

Solution

Python
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.

More iterator & generator programs

← All 20 Iterator & Generator Programs

Leave a Comment