Python iter Function with Examples

The Python function iter is utilized to generate an iterator object. This function constructs an object that allows for iteration over its elements sequentially, one at a time.

Python iter Function Syntax

It has the following syntax:

Example

iter(object, sentinel)

Parameters

  • object: An iterable object.
  • sentinel (optional): It is a special value that represents the end of a sequence.
  • Return

It returns an iterator object.

Python iter Function Example

The example provided below demonstrates how the iter function operates within Python.

Example

# list of numbers

list = [1,2,3,4,5]

listIter = iter(list)

# prints '1'

print(next(listIter))

# prints '2'

print(next(listIter))

# prints '3'

print(next(listIter))

# prints '4'

print(next(listIter))

# prints '5'

print(next(listIter))

Output:

Output

1

2

3

4

5

Explanation:

In the example provided, the iter function transforms a list iterable into an iterator.

Input Required

This code uses input(). Please provide values below: