next() function with iterators in a loop in Python
Question:
How does the next() function interact with iterators in a loop in Python?
- It retrieves the next item from the iterator and advances the iterator to the following item
- It resets the iterator to its initial state
- It skips the next item in the iterator and returns the subsequent one
- It terminates the iteration process immediately
Answer:
A - is the correct answer. The
next() function is used with iterators to retrieve the next item in the sequence. When used in a loop, it advances the iterator to the next item, and when there are no more items, it raises a
StopIteration exception, which can be used to break a loop if not caught