Map, Filter, Reduce
So guys, this is where we start the advance programming in
python.
Map, Filter, and Reduce are paradigms of functional
programming. They allow the programmer (you) to write simpler, shorter code,
without necessarily needing to bother about intricacies like loops and
branching.
MAP
Map comes built-in with python and requires no importing.
The syntax is,
map(function, *iterables)
Where function is
the function on which each element in iterables
(as many as they are) would be applied on. Notice the asterisk (*) on iterables? It means there can be as
many iterables as possible, in so
far function has that exact number as required input arguments.
Before proceeding further, please keep these in mind:-
·
In python 2, the map()
function returns a list. In python 3, the function returns a map object which
is a generator object. In order to obtain list in Python 3 through map (), a built-in list() function can be
called on the map object, list(map(function,
*iterables))
·
The number of arguments to function must be the number of iterables
listed.
Look carefully on these codes,
Simple and length code
Advance plus easier a lot flexible
You will see that these both different code will lead to the same output,
Note-That we
did not call the str.upper function
(doing this: str.upper()), as the
map function does that for us on each element in the pilots_speed list.
What's more important to note is that the str.upper function requires only one
argument by definition and so we passed just one iterable to it. So, if the
function you're passing requires two, or three, or n arguments, then you need
to pass in two, three or n iterables to it
Lets look at another example,
See the beauty of map()? Can you imagine the flexibility this evokes?
The range(1,7)
function acts as the second argument to the round function.
We will now use the zip() function that takes a number of iterables and then creates a tuple containing each of the elements in the iterables.
We can generate the same output with map() function
Look out the output and understand.
FILTER
Filter comes built-in with python and requires no importing.
Unlike map(), filter(), first of all, requires the
function to return Boolean value(true or false) and then passes each element in
the iterable through the function, "filtering" away those that are
false.
Its syntax is,
Before proceeding further to examples there are some points
to be kept in mind:
·
Unlike map(),
only one iterable is required.
·
The function
argument is required to return a boolean type. If it doesn't, filter simply
returns the iterable passed to it. Also, as only one iterable is required, it's
implicit that function must only
take one argument.
·
filter passes each element in the iterable through function and returns only the ones that
evaluate to true.
Lets look at the below example,
Yess, the output will be marks more than 75(excluding 75).
Pretty simple and neat, right?
REDUCE
Reduce applies a function of two arguments cumulatively to
the elements of an iterable, optionally starting with an initial argument.
Reduce needs to be imported as it resides in the functools
module.
Its syntax is,
reduce(function, iterable, initial_value)
Before proceeding further, take a look at these points:-
·
function requires
two arguments,t he first of which is the first element in iterable (if initial
is not supplied) and the second the second element in iterable. If initial is
supplied, then it becomes the first argument to function and the first element in iterable becomes the second
element.
·
reduce
"reduces" (I know, forgive me) iterable into a single value
Now lets look at some examples,
What if you add these numbers through the built-in function
in python , sum().
We will get the same output, 105. We customized our code
similarly the sum() function works.
What if we change the initial value to 10?
Will the output will still be the same? Check out yourself.