Map, Filter, and Reduce Functions || Python Tutorial || Learn Python Programming

Software engineers spend a lot of time
working with lists. You work on Wall Street – you’re analyzing lists of stock
prices. Writing software for a drone delivery service – you’re processing lists
of orders. You work at friendface – you’re profiling lists of users with your
mountain of personal data. A lot of code is spent analyzing, filtering, and
combining the items in a list. Python gives you functions to streamline these
tasks: the map, filter, and reduce functions. We will now iterate over this
list of functions one by one. Let us begin with the map function. Suppose we
have a function that computes the area of a circle with radius R. What if we
need to compute the areas for many different circles? Here is a list of
their radii. One way to do this would be to first create an empty list of areas.
Next, you could loop over the list of radii. For each radius you would compute
the area of the circle, then append it to the area list. This block of code is very
clear and erect. But with the map function, we can accomplish this with a
single line of code. The map function takes two arguments. The first is a
function, and the second is your list, tuple, or other iterable object. Here, map
will apply the area function to each element in the list.
But look! The output of the map function is not a list. It is a map object, which
is actually an iterator over the results. This is highly favorable, especially when
working with large collections of data. We can, however, turn this into a list by
passing the map to the list constructor. If brevity is the soul of wit, then
Python is in a class by itself. Here is the general way the map function works.
Suppose you have a list, tuple, or other iterable collection of data, and we would
like to apply the function f to each piece of data. With the map function, you
first specify the function, then you specify the data to iterate over. The map
function will return an iterator over the collection of F applied to each
piece of data. For a second example, we will use the map
function when working with data for a map. Suppose you work for a weather
reporting service, and all of your temperature data is stored in Celsius.
Then, unexpectedly, someone asks for a weather map in Fahrenheit. How could this
happen? I just do not know. Nevertheless, we live to serve the user. Here is a list
of temperature data for some major cities around the world. Each tuple
contains the name of the city in English, and the temperature in degrees Celsius.
Our goal is to convert this to a list, where the degrees are in Fahrenheit. Here
is the formula for converting from Celsius to Fahrenheit. First, we will
write a converter function using a lambda expression. This function will
accept a tuple as the input, and will return a tuple with the same name, but
the temperature in Fahrenheit. If you are unfamiliar with lambda expressions, they
are a way to create short functions in a single line. We have a video that covers
this topic in detail. You can now create a list of data in Fahrenheit, by mapping
the converter function to our list of temperature data. Do not forget your
jacket if you’re visiting Argentina. The filter function is used to select
certain pieces of data from a list, tuple, or other collection of data. This name
was chosen because it filters out the data you do not need. Suppose you were
analyzing some data and you would like to select all values that are above the
average. First, import the statistics module since it contains the mean
function. Here is a short list of data that I collected from a nearby fuel
sensor. Let us compute the average of this data, then display the average so
that you can see that the filter works as intended. We will now use the filter
function to select the data greater than the average. Just like with map, the first
argument is a function. We will create an anonymous function that tests the input
to see if it is above average. Next, pass in the list of data. The filter function
will only return the data for which the function is true. Once again, the return
value is not a list, but a filter object, which is an iterator over the results. We
can create a list of the results by passing the filter function to the list
constructor. It worked. These are the three values
above the mean, and with a small change to the lambda expression, you can select
the values below average. I should check the sensor. We cannot have negative
amounts of fuel. Can we? Let us see another interesting use of the filter
function. When you are working with data from outside sources, you will often
encounter missing data or empty values. For example, here is a partial list of
the countries in South America. Notice there are numerous strings that are
empty. To remove these, we’ll use the filter function. But instead of passing
in a function, we’ll simply pass none as the first argument. The second argument
is the list of data as before. This filters out all values that are treated
as false in a boolean setting. In Python, the values that are treated as false are
the empty string, 0, an empty list, empty tuple, empty dictionary, false, none, and
those objects that signal to Python that it is a trivial instance. But be careful
when using the filter function in this way. 0 is a valid piece of data in most
situations, so you would not want to filter that out. The last function on our
list is the reduce function. This function is a bit …unusual. In fact, as of
Python 3, it is no longer a built-in function. Instead, it has been demoted to
the func tools module. The creator of Python had this to say about the reduce
function: use func tools dot reduce if you really need it; however, 99% of the
time an explicit for loop is more readable.
Once you see an example of the reduced function you are likely to agree. Here is
how the reduced function works. Suppose you have a sequence of data, and a
function that takes two arguments. The reduce function will first apply F to
the first two terms. Next, it will apply F to the output value and to the third
piece of data. Then, it will repeat this process. In each step, it applies F to the
previous output value and the next term in the sequence. Once it has reached the
last piece of data, it will return the final value.
Alternatively, the reduced function computes this nested value. Let us see an
example. To use the reduce function, we must first import it from the func tools
module. Let us use the reduce function to multiply all numbers in a list. For this
demonstration, we will multiply the first 10 prime numbers. To use the reduce
function, we need a function that takes two inputs. We will use a lambda
expression to make a function that will multiply the two inputs. And there is the
product. Here is how it would look if you multiply the prime numbers using a for
loop instead of the reduce function Which do you prefer? The map, filter, and
reduce functions greatly simplify the process of working with lists and other
iterable collections of data. In fact, if you use lambda expressions, your work can
often be done in a single line. After you master these functions you will realize
that Python should be a comedian, because it’s full of one-liners.


Add a Comment

Your email address will not be published. Required fields are marked *