Many situations will arise where we have to access an object like an iterator. One of the ways to do this is to form a generator loop, but, this method will extend the task and also consume more time. In Python for Data Science, we have a built-in method _iter_() for performing this task.
The function _iter_() will return an iterator for an object (set, tuple, array, etc., or custom objects). This function creates an object that is accessible element by element. For accessing the object by each element, the function _next_() is use. This is very useful when we are dealing with loops.
- Object – Here, the object whose iterator has to be created is mentioned. The object can be a collection object like a list or a user-defined object.
- Callable, sentinel – Callable points out a callable object. Sentinel value is the value at which the iteration should terminate. The sentinel value denotes the end of the sequence, which is being iterated.
- Calling the iterator after iteration of all elements will result in the raising of StopIteration Error.
- The original object is not modified.
Example 4: User defined objects.