sions (or list comprehensionsand map(and filter(is most often purely matter of personal programming style using map()filter()and functools reduce(often leads to the elimination of loopsas the examples we have seen illustrate these functions are useful when converting code written in functional languagebut in python we can usually replace map(with list comprehension and filter(with list comprehension with conditionand many cases of functools reduce(can be eliminated by using one of python' built-in functional functions such as all()any()max()min()and sum(for examplesum(os path getsize(xfor in files if endswith(py")this achieves the same thing as the previous three examplesbut is much more compact operator attrgetter( in addition to providing functions for python' operatorsthe operator module also provides the operator attrgetter(and operator itemgetter(functionsthe first of which we briefly met earlier in this both of these return functions which can then be called to extract the specified attributes or items whereas slicing can be used to extract sequence of part of listand slicing with striding can be used to extract sequence of parts (sayevery third item with [:: ])operator itemgetter(can be used to extract sequence of arbitrary partsfor exampleoperator itemgetter( )(lthe function returned by operator itemgetter(does not have to be called immediately and thrown away as we have done hereit could be kept and passed as the function argument to map()filter()or functools reduce()or used in dictionarylistor set comprehension when we want to sort we can specify key function this function can be any functionfor examplea lambda functiona built-in function or method (such as str lower())or function returned by operator attrgetter(for exampleassuming list holds objects with priority attributewe can sort the list into priority order like thisl sort(key=operator attrgetter("priority")in addition to the functools and operator modules already mentionedthe itertools module can also be useful for functional-style programming for examplealthough it is possible to iterate over two or more lists by concatenating theman alternative is to use itertools chain(like thisfor value in itertools chain(data_list data_list data_list )total +value the itertools chain(function returns an iterator that gives successive values from the first sequence it is giventhen successive values from the second sequenceand so on until all the values from all the sequences are used the itertools module has many other functionsand its documentation gives many