Python Reference Library →

Python iter() Function – Create an Iterator from Iterable

Let’s look at iter() function – create an iterator from iterable

  1. Notice

    iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().

  2. Try

    Follow the example, change its sample, and compare what happens.

  3. Check

    Explain the result and test your understanding.

Watch how it works

Focus on how the names, objects, and calculations change. We’ll inspect the printed output in the next section.

Explore the idea

Follow the code and watch its state change. Guided simulation.

Code
Python
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))
Live state

Top-level scope

items→Object 1
iterator→Object 2
Object 1 · rangerange(0, 3)Object 2 · range_iterator<range_iterator object>
This step

iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().

Picture iter() function – create an iterator from iterable

iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().

Image brief

Alt text
A diagram for Python iter() Function – Create an Iterator from Iterable. Instructions: items = range(3) iterator = iter(items) print(next(iterator, "done")) print(next(iterator, "done")). Displayed result: 0 1. The final example bindings are items --> object 1 (range); iterator --> object 2 (range_iterator). object 1: range(0, 3); object 2: <range_iterator object>
Image generation prompt
Create a clean educational illustration for a Python programming lesson titled "Python iter() Function – Create an Iterator from Iterable". Teaching focus: iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next(). Depict exactly the relationships and labels in this reference diagram: 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> Use a flat vector style, generous spacing, clear arrows and legible labels, with a restrained blue/teal palette on a plain background. The diagram must explain the programming idea accurately. Do not depict variables as boxes containing copied objects when they share a reference. Avoid mascots, decorative screenshots, logos, gradients and extra text. Landscape composition, approximately 3:2, suitable for a course card. Accessible description: A diagram for Python iter() Function – Create an Iterator from Iterable. Instructions: items = range(3) iterator = iter(items) print(next(iterator, "done")) print(next(iterator, "done")). Displayed result: 0 1. The final example bindings are items --> object 1 (range); iterator --> object 2 (range_iterator). object 1: range(0, 3); object 2: <range_iterator object>

Follow the execution

Choose a different sample input, then follow every step through to the output. Before revealing it, predict what the program will display.

Execution workspace

Choose a sample and follow its execution. Guided simulation.

Code
Python
items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))
Live state

Top-level scope

items→Object 1
iterator→Object 2
Object 1 · rangerange(0, 3)Object 2 · range_iterator<range_iterator object>
This step

The example has finished. Compare the final output with the values and references above.

Terminal
Output
0
1

What do you expect this program to print?

What does this example display?

items = range(3)
iterator = iter(items)
print(next(iterator, "done"))
print(next(iterator, "done"))
What does this example display?
Choose one answer. You can try again.

Answer and explanation

0 1

Follow the printed values in order. The output is: 0 1

Which explanation matches what you learned?

Which explanation matches what you learned?
Choose one answer. You can try again.

Answer and explanation

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().

More examples and reference

Python iter() Function

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

Syntax

iter(iterable)

or

iter(callable, sentinel)

Parameters:

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

Returns:

  • An iterator object.

Example: Using iter() with a List

my_list = [10, 20, 30]
it = iter(my_list)

print(next(it))  # 10
print(next(it))  # 20
print(next(it))  # 30
10
20
30

Example: Looping with iter()

words = ["hello", "world"]
it = iter(words)

for word in it:
    print(word)
hello
world

Example: Using iter() with Callable and Sentinel

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

Use Case: Why Use iter()?

  • Used behind the scenes in for loops
  • Manual control over iteration using next()
  • Custom data streaming or lazy evaluation

Common Mistakes

  • Trying to use next() on an object that is not an iterator
  • Forgetting that iter() does not copy the object; it returns a pointer-like object

Interview Tip

When asked about iterables vs iterators, remember: iter() converts an iterable into an iterator, which then maintains internal state across next() calls.

Summary

  • iter() is used to create an iterator from an iterable or callable
  • Works with lists, strings, tuples, dictionaries, and more
  • Returns an object compatible with next()

Practice Problem

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

What to remember

Connect what you noticed in the example with the idea below.

Remember

iter() obtains an iterator from an iterable. The resulting iterator advances independently as you call next().

Watch out for this idea

iter() always returns a list.

This is the misconception to avoid.

Reference checks are optional and do not count toward course progress.

    Reference library →