In this tutorial we will learn about the python iter() method and its uses with examples.

What is Python iter() Method?
The iter() is a Python built-in method that returns an iterator for the given object.
The syntax of the iter() method is:
Python iter() Method Parameters
The iter() methods take two parameters as an argument:
- object – the name of the object whose iterator has to be returned.
- sentinel (optional) – A numeric value that is used to represent the end of the sequence.
Let’s check some examples of the python iter() method.
Example 1: How to use the iter() method in python?
Output:
We can use the next() method to print the elements of iteration, and the iteration will remember the next count via the internal count variable. Once the iteration is complete, it raises a StopIteration exception, and the iteration count cannot be reassigned to 0.
We can also use __next__() method with ithe ter() method to print the iterator.
Example 2: Using __next__() method in python iter() method.
The output will be as follows.
Example 3: Using iter() with custom objects.
The output will be as follows.
Rules of iter()
- If the user-defined object doesn’t implement __iter__(), and __next__() or __getitem()__, the TypeError exception is raised.
- If the sentinel parameter is also provided, iter() returns an iterator until the sentinel character isn’t found.