[Solved] What is the use of iter in python?


First of all: iter() normally is a function. Your code masks the function by using a local variable with the same name. You can do this with any built-in Python object.

In other words, you are not using the iter() function here. You are using a for loop variable that happens to use the same name. You could have called it dict and it would be no more about dictionaries than it is about Guido’s choice of coffee. From the perspective of the for loop, there is no difference between your two examples.

You’d otherwise use the iter() function to produce an iterator type; an object that has a .next() method (.__next__() in Python 3).

The for loop does this for you, normally. When using an object that can produce an iterator, the for statement will use the C equivalent of calling iter() on it. You would only use iter() if you were doing other iterator-specific stuff with the object.

solved What is the use of iter in python?