Remember
iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().
iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().
Follow the example, change its sample, and compare what happens.
Explain the result and test your understanding.
Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.
Follow the code and watch its state change. Guided simulation.
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))Top-level scope
range(0, 3)Object 2 · range_iterator<range_iterator object>iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().
(no output)
What do you expect this program to print?
EXAMPLE INSTRUCTIONS
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))
|
v
DISPLAYED RESULT
0
1
FINAL BINDINGS
items --> object 1 (range)
iterator --> object 2 (range_iterator)
object 1: range(0, 3)
object 2: <range_iterator object>Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.
Choose a sample and follow its execution. Guided simulation.
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))Top-level scope
range(0, 3)Object 2 · range_iterator<range_iterator object>The example has finished. Compare the final output with the values and references above.
0 1
What do you expect this program to print?
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))iter() FunctionThe iter() function in Python is used to create an iterator from an iterable (like a list, tuple, or string). It plays a key role in enabling for-loops, comprehensions, and lazy evaluation.
iter(iterable)
or
iter(callable, sentinel)
iterable – Any object capable of returning its elements one at a time (like a list, string, tuple, etc.)callable, sentinel – An alternate form used to create an iterator from a function until a specific value (sentinel) is returned.my_list = [10, 20, 30]
it = iter(my_list)
print(next(it)) # 10
print(next(it)) # 20
print(next(it)) # 30
10
20
30
words = ["hello", "world"]
it = iter(words)
for word in it:
print(word)
hello
world
# Read input until the user types 'stop'
def get_input():
return input("Enter value: ")
for val in iter(get_input, 'stop'):
print("You entered:", val)
Explanation: This form of iter() calls the function repeatedly until it returns the sentinel value.
for loopsnext()next() on an object that is not an iteratoriter() does not copy the object; it returns a pointer-like objectWhen asked about iterables vs iterators, remember: iter() converts an iterable into an iterator, which then maintains internal state across next() calls.
iter() is used to create an iterator from an iterable or callablenext()Create your own list of numbers and use iter() with next() to print each number one-by-one.
nums = [1, 2, 3, 4]
it = iter(nums)
while True:
try:
print(next(it))
except StopIteration:
break
Connect what you noticed in the example with the idea below.
iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().
iter() always returns a list.
This is the misconception to avoid.Reference checks are optional and do not count toward course progress.