At many instances, we get a need to access an object like an iterator. One way is to form a generator loop but that extends the task and time taken by the programmer. Python eases this task by providing a built-in method __iter__() for this task.
The __iter__() function returns an iterator for the given object (array, set, tuple etc. or custom objects). It creates an object that can be accessed one element at a time using __next__() function, which generally comes in handy when dealing with loops.
iter(object) iter(callable, sentinel)
- Object : The object whose iterator has to be created. It can be a collection object like list or tuple or a user-defined object (using OOPS).
- Callable,Sentinel : Callable represents a callable object and sentinel is the value at which the iteration is needed to be terminated, sentinel value represents the end of sequence being iterated.
If we call the iterator after all the elements have been iterated, then StopIterationError is raised.
The __iter__() function returns an iterator object that goes through the each element of the given object. The next element can be accessed through __next__() function. In the case of callable object and sentinel value, the iteration is done until the value is found or the end of elements reached. In any case, the original object is not modified.
Code #1 :
a e i o u
Code #2 :
11 22 33 44 55
Code #3 :
Cat Bat Sat Mat Throwing 'StopIterationError' I cannot count more.
Code #4 : User-defined objects (using OOPS)
Print the range without iter() Eating more Pizzas, couting 2 Eating more Pizzas, couting 3 Eating more Pizzas, couting 4 Eating more Pizzas, couting 5 Print the range using iter() Eating more Pizzas, couting 2 Eating more Pizzas, couting 3 Eating more Pizzas, couting 4 Eating more Pizzas, couting 5 Dead on overfood, GAME OVER
- Python | iter() method
- Converting WhatsApp chat data into a Word Cloud using Python
- Python | Converting all strings in list to integers
- Converting an image to ASCII image in Python
- Converting Color video to grayscale using OpenCV in Python
- Python | Converting list string to dictionary
- Python | Converting String content to dictionary
- Converting Roman to Decimal in Python using gnboorse-rom module
- Object Oriented Programming in Python | Set 1 (Class, Object and Members)
- Object Oriented Programming in Python | Set 2 (Data Hiding and Object Printing)
- Python | Difference between iterable and iterator
- Converting Power Law Distribution to a Linear graph
- Converting Series of lists to one Series in Pandas
- Iterator Functions in Python | Set 1
- Iterator Functions in Python | Set 2 (islice(), starmap(), tee()..)
- Python | range() does not return an iterator
- Python PIL | ImageSequence.Iterator()
- Iterator Method - Python Design Patterns
- Ways to increment Iterator from inside the For loop in Python
- Python | Convert dictionary object into string
This article is contributed by Harshit Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.