next() — Python Technical Interview Questions & Answers
next()
next()
- next() is built into Python. You do not import it. Freshers only need one honest call and the printed result — not a long definition.
- next() — output: 1 then 2. Default avoids StopIteration.
- Wrong type often raises TypeError. Empty values matter for bool/all/any. Don’t mix next up with a similar name (sorted vs sort, int vs float).
Technical Interview Tip: Call next() once and say what prints.
Code Examples for next()
Example
# next()
it = iter([1, 2])
print(next(it))
print(next(it, "end"))
next() — output: 1 then 2. Default avoids StopIteration.
How to Explain next() to an Interviewer
Explain next() as if you are teaching a junior — definition, then one tiny script.
next() is built-in — no import. One call + exact output.
What does the example print, and why?
next() — output: 1 then 2
What mistake do freshers make with next()?
Wrong type → TypeError.