You’ll likely find yourself needing to perform an operation on all the items in a collection. For instance, let’s say you want to add a 10% discount to the price of all the items in a Python list. To do so, you could iterate through the list using a for loop and apply discounts to individual items. However, with Python Map, Filter, and Reduce functions, performing operations on individual list items becomes lot easier and this is what we’re going to explore today.
Python Map Function
The map function is best explained with an example. Suppose you have a list of integers that contain original prices of some items. You want to create a new list that contains discounted prices for the same items. To do so, one of the options is to create a function that returns the discount on an item price passed to it. Next, you can iterate through the original list using a for loop, passing individual items to the function and returning discounted prices for each one and storing the returned values to a new list. Let’s see how to do this.
The following script defines find_discount()
, which takes 15% off the original value passed to it.
def find_discount(total_price):
return (total_price - (0.15 * total_price))
Our next script defines a list of item prices, find_discount()
function and adding the returned values to our
item_prices = [120, 200, 300, 180, 500]
item_discounts = []
for i in item_prices:
item_discounts.append(find_discount(i))
print(item_discounts)
Output:
[102.0, 170.0, 255.0, 153.0, 425.0]
Though the above method is self-explanatory, a for loop
can get extremely slow while iterating through millions of items in a collection. This is where the Map function comes in. With the Map function, you can achieve the above task in a single line of code.
The syntax of the map function is as follows:
#map(function, list_items)
#[function(list_items[0]), function(list_item[]), function(list_item[2]) .... function(list_item[n-1])]
The map function basically applies the function, which is passed to it as the first parameter, to each of the items in the collection passed to it as the second parameter, and returns the collection of values.
So, you can calculate the discounted prices for items in the items_price
list using the Map function with one line of code. The Map function returns an iterator which you can convert to a list using the list()
function as shown in the following script.
item_discounts2 = map(find_discount, item_prices)
print(list(item_discounts2))
The output confirms we get the same list of discounted prices.
Output:
[102.0, 170.0, 255.0, 153.0, 425.0]
The Map function can also be used with lambda functions. Lambda functions are anonymous functions with a single expression. The following script defines a lambda expression with the same functionality as the find_discount()
function we wrote earlier. A for loop iterates through the
result = lambda x: x - (0.15 * x)
item_prices = [120, 200, 300, 180, 500]
item_discounts = []
for i in item_prices:
item_discounts.append(result(i))
print(item_discounts)
Output:
[102.0, 170.0, 255.0, 153.0, 425.0]
So, how can the Map function use this lambda expression to perform the same operation in a single line of code? Take a look:
item_prices = [120, 200, 300, 180, 500]
item_discounts2 = map(lambda x: x - (0.15 * x), item_prices)
print(list(item_discounts2))
In the above script, you can see that the lambda expression has been passed as the first parameter of the Map function which applies the function to all the items in the
Output:
[102.0, 170.0, 255.0, 153.0, 425.0]
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.
Python Filter Function
A filter function is used to evaluate all the individual list items against a boolean condition and returns the items for which the boolean condition is true.
Like the Map function, you can pass lambda expressions to a Filter function. In the following script, the Filter function applies a lambda expression to all the items in the
expensive_items = filter(lambda x: x > 200, item_prices)
print(list(expensive_items))
Output:
[300, 500]
Let’s try to pass the same lambda expression we used in the Filter function in our last example to a Map function and study the results.
expensive_items = map(lambda x: x > 200, item_prices)
print(list(expensive_items))
Output:
[False, False, True, False, True]
From the output you can see that instead of returning the actual filtered values, the Map function returns boolean value for each of the items in the list. The Filter function actually correlates these boolean values to their original values so the values that satisfy the evaluation criteria are returned.
You can also use the Filter function to filter the values that are not null in a collection. For example, you can filter all the non-empty strings from a list of strings using the Filter function as shown in the following script. You simply have to pass None
as a function parameter to the Filter function.
names = ["", "Jon", "Rose", "", "Nick", "Sally"]
not_none = filter(None, names)
print(list(not_none))
Output:
['Jon', 'Rose', 'Nick', 'Sally']
Note: Empty strings are evaluated as false when used in a boolean expression.
Python Reduce Function
The Reduce function is unique in its application in the way that it first applies a function to the first two items in a collection. In the next iteration, the same function is applied on the the output of the first iteration, and the next item in the collection. This sounds confusing, but the following expression hopefully makes it clear:
items = [x1, x2, x3, x4, x]
result = reduce(f, items)
result =f(f(f(f(x1,x2),x3),x4),x5)
Before we run through an example, it’s important that, unlike the Map and Filter functions, the Reduce function needs to be imported in order to access it. The following line imports the reduce
function from the functools
module, which is a collection of higher-order functions.
from functools import reduce
Now, let’s try to find the factorial of a number using the Reduce function. Since a factorial of a number N is the multiple of all the numbers from 1 to N, you can use the Reduce function to calculate factorial of a number.
To demonstrate this, let’s calculate 5 factorial. For that, you need to first create a list of integers from 1 to 5. The following script does that easily enough.
items = list(range(1,6))
print(items)
Output:
[1, 2, 3, 4, 5]
Next, you can use a lambda expression that multiplies the two items passed to it as parameters. You’ll use that lambda expression in the Reduce function to find the factorial of a particular number as shown below:
from functools import reduce
items = list(range(1,6))
fact = reduce(lambda x,y: x * y, items)
print(fact)
Output:
120
Internally, the Reduce function performs the following calculation on the items list:
fact = ((((1 * 2) * 3) * 4) * 5)
You can see that it reduces the values in our list based on the lambda function we applied. The first two numbers are passed through our lambda function, then each result is passed with the next number in the list to our lambda function until we’ve reduced the entire list.
The Reduce function is quite powerful. You can use it to find the sum of the values in a list or even to find the maximum or minimum values in a list using a conditional lambda expression, like this:
from functools import reduce
items = list(range(1,6))
maxval = reduce(lambda x,y: x if x > y else y, items)
print(maxval)
Code More, Distract Less: Support Our Ad-Free Site
You might have noticed we removed ads from our site - we hope this enhances your learning experience. To help sustain this, please take a look at our Python Developer Kit and our comprehensive cheat sheets. Each purchase directly supports this site, ensuring we can continue to offer you quality, distraction-free tutorials.