Menu Close

Python iter() Function

python iter function

In this article, we are going to learn all about the python iter() function to return an iterator object. Here we will understand the python built-in iter() function to create an iterator object and access all the items of an iterable using an iterator object.

Python iter function

Python iter function is a built-in function in Python that is used to return an iterator object to iterate each item of the iterable by using the next() function and for loop.

Syntax

The syntax of the python iter() function is:-

iter(object, sentinel)

Parameter

iter function in Python accepts two parameters. Which are given below

  • object:– Required, an iterable object.
  • sentinel:- Optional, If the object is a callable object The iteration will stop when the return value same as the sentinel.

Return Value

The return value of the Python iter() function is the iterator object.

Python iter example

Here we will see some examples of understanding the Python iter() function so that you can get more clear about the iter() function in Python and its use cases.

Example:- Python iterator object with next() function


my_list = ['Python', 'Java', 'C++', 'C', 'PHP']

# create iterator object
my_iterator = iter(my_list)

# access all the items of my_list using my_iterator object and next() function.
print(next(my_iterator)) 
print(next(my_iterator)) 
print(next(my_iterator)) 
print(next(my_iterator)) 
print(next(my_iterator))

Output

Output:
Python
Java
C++
C
PHP

Example:- Using iterator object with Python for loop

Iterator object can be used with Python for a loop. Using Python for loop you can get all the items of the iterable.


# list of names
lst = ["Vishvajit", "Vinay", "Pankaj", "Harshita", "Pankhudi"]

# creating iterator object
iterator = iter(lst)

# iterate each item of the iterable
for i in iterator:
    print(i)

Output

Vishvajit
Vinay
Pankaj
Harshita
Pankhudi

As you can see in the above example, How can I get each item of the iterable ( lst ) with the help of the Python for loop?

Example: Creating Custom Iterator

In this example, we are creating a custom iterator instead of using built-in.


class CalculateSquare:

    # constructor of the class
    def __init__(self, max):
        self.max = max

    # iter method of the class
    def __iter__(self):
        self.num = 1
        return self

    # next method of the class
    def __next__(self):
        if self.num <= self.max:
            x = f"Square of {self.num} will be:- {self.num * self.num}"
            self.num += 1
            return x
        raise StopIteration


# creating object of the class
obj = CalculateSquare(10)

# creating iterable object
iterator_object = iter(obj)

# iterate each item
for i in iterator_object:
    print(i)

Output

Square of 1 will be:- 1
Square of 2 will be:- 4
Square of 3 will be:- 9
Square of 4 will be:- 16
Square of 5 will be:- 25
Square of 6 will be:- 36
Square of 7 will be:- 49
Square of 8 will be:- 64
Square of 9 will be:- 81
Square of 10 will be:- 100

Conclusion

In this article, you have learned all about the Python iter() function to create the iterator object. An iterator is an object in python that can be iterated upon, meaning an iterator object contains a countable number of values. You can access all the items of iterable passing the iterator object in the next() function.

If this article helps you, please comment and keep visiting for further python built-in functions tutorials.

Python built-in functions


For more information:- Click Here

Thanks for your valuable time … ❤️❤️🧑‍💻🧑‍💻

Previous Article
Next Article

Related Posts