Python Practice › Iterator & Generator Programs › Program 1
Use iter() and next()
Write a Python program that walks through a list with iter() and next().
Output
login search logout no more steps
Approach
next()returns items one by one; a default avoids StopIteration.
Solution
it = iter(["login", "search", "logout"])print(next(it))print(next(it))print(next(it))print(next(it, "no more steps"))Output
login search logout no more steps
Learn the concept
New to this topic? Read the lesson, then come back.