Introduction to Python Reduce
The module used to define the Reduce function is functools. Like filter() and map() functions, reduce receives two arguments. Reduce function doesn’t return an iterable, instead, it returns a single value. Reduce is a function that executes a specific function of elements. These functions should be iterables. Functions send parameters as items.
Reduce() function returns a map object. A function that is to be passed to all the elements of the list is applied in its arguments. Lambda functions are used to make the code more readable and other operator functions. In this topic, we are going to learn about Python Reduce.
Syntax of Python Reduce
reduce (function, iterable)
Terms of syntax:
- Function obligation to be executed for each item.
- Iterable is a sequence, a collection. It returns an iterator object as many iterables as one desire can be sent.
- Function consists of one parameter for each iterable.
Engineers today, are predominantly busy working and dealing with lists. Let’s say:
You work in Wall Street- you are 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. One such function is reduced ().
Map, Filter and Reduce functions are built-in high order functions in Python.
Often, using a generator expression over a reduced function is preferred. User preference is involved. Defining a function inline using lambda, the preferential expression is the generator and will be clean over the reduce function.
Functions usage in general – Python:
What are Lambda Expressions?
A lambda expression is an anonymous, in-line declaration of a function, usually passed as an argument. Lambda functions can do whatever a usual function or a regular function can. The exception is that the lambda functions, from anywhere outside, cannot be called. Meaning, where it is defined. So-called anonymous for the same reason.
Lambda functions are quite useful when you require a short, throwaway anonymous function. Simplicity is that it can be used only once. Applied explicitly near the sorting and filtering of the data.
lambda arguments: expression
Type Lambda keyword and then zero and other more inputs.
Just like functions, it is perfectly acceptable to have anonymous functions with no inputs.
Next, type a colon. Then finally you enter a single expression. This expression is a return value. Multi-line functionality or more than a single line is not possible using such functions.
A Functional Preview Of Reduce
Data: a1, a2, a3,..,an Function: f reduce (f, data) :
- Suppose you have a list/tuple /other iterable collection of data, (consider Data: a1, a2, a3…..,an. as it for time being)
- Each piece of data is applied with the function: f.
- With the reduce function(reduce (f, Data):), you first specify the function and then the data to iterate over.
- The reduce function will iterate over the collection (f(a1), f(a2,),…., f(an)) of f applied to each piece of data.
- The result is obtained by picking the first two elements of a sequence.
- The previously attained result is applied with the same function and the number next to the second element, the result is cached.
- Till container is left with no more elements, the process continues.
- The console returns the end result.
Examples of Python Reduce
1. Let’s say we multiply a list through reduce function. You try the same with no reduced function and assess which is slick.
- First consider importing the tool functools and reducing function from within it.
- Now, for performing the multiplication, give a list of desired values.
- Lambda function is used as discussed above, why and how it is used.
- If you simply print the multiplier, it returns the reduce object.
- We need to Convert the reduce object into a return result of which we are looking for.
from functools import reduce #Multiplication of all the numbers in the list data = [2, 3, 5, 7, 9, 11, 17, 19, 23, 29] multiplier = lambda x, y : x*y print(reduce (multiplier, data))
2. Here, in the next example, we try to find the factorial of a given number using reduce function.
import functools def multiply(a,b): print("a=",a," b=",b) return a*b factorial=functools.reduce(multiply, range(1, 6)) print ('Factorial of 5 is: ', factorial)
3. Sum of given numbers is performed in the below example using reduce function.
from functools import reduce def do_sum(x1, x2): return x1 + x2 print(reduce(do_sum, [1, 2, 3, 4]))
The map, filter and reduce functions greatly simplifies 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 Python should be a comedian because it is full of one-liners.
This is a guide to Python Reduce. Here we discuss a Functional Preview Of Reduce and Examples of Python Reduce along with the outputs. You may also have a look at the following articles to learn more –