advanced programming techniques the filter(function can always be replaced with generator expression or with list comprehension reducing involves taking function and an iterable and producing single result value the way this works is that the function is called on the iterable' first two valuesthen on the computed result and the third valuethen on the computed result and the fourth valueand so onuntil all the values have been used the functools module' functools reduce(function supports this here are two lines of code that do the same computationfunctools reduce(lambda xyx [ ]functools reduce(operator mul[ ]returns returns the operator module has functions for all of python' operators specifically to make functional-style programming easier herein the second linewe have used the operator mul(function rather than having to create multiplication function using lambda as we did in the first line python also provides some built-in reducing functionsall()which given an iterablereturns true if all the iterable' items return true when bool(is applied to themany()which returns true if any of the iterable' items is truemax()which returns the largest item in the iterablemin()which returns the smallest item in the iterableand sum()which returns the sum of the iterable' items now that we have covered the key conceptslet us look at few more examples we will start with couple of ways to get the total size of all the files in list filesfunctools reduce(operator add(os path getsize(xfor in files)functools reduce(operator addmap(os path getsizefiles)using map(is often shorter than the equivalent list comprehension or generator expression except where there is condition we've used operator add(as the addition function instead of lambda xyx if we only wanted to count the py file sizes we can filter out non-python files here are three ways to do thisfunctools reduce(operator addmap(os path getsizefilter(lambda xx endswith(py")files))functools reduce(operator addmap(os path getsize( for in files if endswith(py")))functools reduce(operator add(os path getsize(xfor in files if endswith(py"))arguablythe second and third versions are better because they don' require us to create lambda functionbut the choice between using generator expres