What are some of the built-in functions in Python that return an iterator?

  • Are there any built-in functions that can return an iterator object?
  • Are there any other ways to to get an iterator object in Python?

The following are built-in functions in Python that return an iterator object.

To know what iterators are, click on this link.

enumerate( iterable , start=0 )
Returns an enumerate object. iterable must be a sequence, an iterator, or some other object which supports iteration. The __next__() method of the iterator returned by enumerate() returns a tuple containing a count (from start which defaults to 0) and the values obtained from iterating over iterable .

filter( function , iterable )
Constructs an iterator from those elements of iterable for which function returns true. iterable may be either a sequence, a container which supports iteration, or an iterator. If function is None , the identity function is assumed, that is, all elements of iterable that are false are removed.

Note that filter(function, iterable) is equivalent to the generator expression (item for item in iterable if function(item)) if function is not None and (item for item in iterable if item) if function is None .

iter( object [, sentinel ])
Returns an iterator object. The first argument is interpreted very differently depending on the presence of the second argument. Without a second argument, object must be a collection object which supports the iteration protocol (the __iter__() method), or it must support the sequence protocol (the __getitem__() method with integer arguments starting at 0 ). If it does not support either of those protocols, TypeError is raised. If the second argument, sentinel , is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its __next__() method; if the value returned is equal to sentinel , StopIteration will be raised, otherwise the value will be returned.

map( function , iterable , )
Return an iterator that applies function to every item of iterable , yielding the results. If additional iterable arguments are passed, function must take that many arguments and is applied to the items from all iterables in parallel. With multiple iterables, the iterator stops when the shortest iterable is exhausted.

reversed( seq )
Return a reverse iterator. seq must be an object which has a __reversed__() method or supports the sequence protocol (the __len__() method and the __getitem__() method with integer arguments starting at 0 ).

zip( *iterables )
Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the i -th tuple contains the i -th element from each of the argument sequences or iterables. The iterator stops when the shortest input iterable is exhausted. With a single iterable argument, it returns an iterator of 1-tuples. With no arguments, it returns an empty iterator.

The itertools module contains a number of commonly-used iterators as well as functions for combining several iterators. One can check out the Python documentation here.

Generator Functions and Generator expressions can also be used to build/get iterator objects in Python, one can click on this link to find out more.

1 Like