6.2 Calling functions on elements The $_{operator}$module contains a set of functions corresponding to Python’s operators. Some examples are operator.add(a, b) (adds two values), operator.ne(a, b) (same as a != b ), and operator. attrgetter('id') (returns a callable that fetches the$_{.id}$attribute). itertools.starmap(func, iter) assumes that the iterable will return a stream of tuples, and calls func using these tuples as the arguments: itertools.starmap(os .path.join, [('/bin','python'), ( '/usr','bin','java'), ('/usr','bin','perl'), ( '/usr','bin','ruby')]) => /bin/python,/usr/bin/java,/usr/bin/perl,/usr/bin/ruby Another group of functions chooses a subset of an iterator’s elements based on a predicate. 6.3 Selecting elements itertools.filterfalse(predicate, iter) is the opposite of filter() , returning all elements for which the predicate returns false: itertools.filterfalse(is_even, itertools .count()) => 1,3,5,7,9,11 ,13 ,15 ,... itertools.takewhile(predicate, iter) returns elements for as long as the predicate returns true. Once the predicate returns false, the iterator will signal the end of its results. def less_than_10 (x): return x <10 itertools.takewhile(less_than_10, itertools .count()) => 0,1,2,3,4,5,6,7,8,9 itertools.takewhile(is_even, itertools .count()) => 0 itertools.dropwhile(predicate, iter) discards elements while the predicate returns true, and then returns the rest of the iterable’s results. itertools.dropwhile(less_than_10, itertools .count()) => 10 ,11 ,12 ,13 ,14 ,15 ,16 ,17 ,18 ,19 ,... itertools.dropwhile(is_even, itertools .count()) => 1,2,3,4,5,6,7,8,9,10 ,... itertools.compress(data, selectors) takes two iterators and returns only those elements of data for which the corresponding element of selectors is true, stopping whenever either one is exhausted: itertools.compress([1,2,3,4,5], [ True , True , False , False , True ])=> 1,2,5 13