Reduce Function in Python
What is Reduce Function?
The reduce() function is used to reduce an iterable (e.g., a list or tuple) to a single value by performing a specified operation on each element. The basic syntax of reduce() is:
reduce(function, iterable[, initial])
The iterable argument is iterable that will be reduced. The function argument is a function that takes two arguments and returns a single value. The first argument is the accumulated value (the result of the previous function call), and the second is a current element of the iterable. The optional initial argument is the initial value of the accumulated value, and if it is not provided, the first element of the iterable is used as the initial value.
Some examples to understand how to reduce() works:
Finding the Sum of a List of Numbers using reduce():
EXAMPLE:
from functools import reduce
numbers = [1, 2, 3, 4, 5]
sum = reduce(lambda x, y: x + y, numbers)
print("The sum is:",sum)
OUTPUT:
The sum is: 15
CODE EXPLANATION:
This code uses the reduce() function to calculate the sum of the numbers in the numbers list. The reduce() function has two arguments: a lambda function that takes two arguments, x and y, and returns their sum (x + y) and the numbers list. reduce(), then applies the lambda function to each pair of elements in the list, starting with the first two elements (1 and 2). It adds them together, producing a result of 3, which becomes the first argument x in the next iteration. The next element in the list (3) is then used as the second argument y to the lambda function, which returns a sum of 6. This process is repeated for each subsequent element in the list, accumulating the sum of all elements. Finally, the reduce() function returns the single accumulated value of 15.
The resulting sum is assigned to a variable called sum, which is then printed to the console using the print() function.
Finding the Maximum Number in a List using reduce():
EXAMPLE:
from functools import reduce
numbers = [5,51,12,7,8]
max_num = reduce(lambda x, y: x if x > y else y, numbers)
print(“The maximum number among the list is:”,max_num)
OUTPUT:
The maximum number among the list is: 51
CODE EXPLANATION:
This code finds the maximum number in a list of numbers using the reduce() function from the functools module in Python. First, a list of numbers is defined: numbers = [5, 51, 12, 7, 8]. Next, the reduce() function is called with a lambda function as its first argument. The lambda function takes two arguments, x and y, and returns the larger of the two: lambda x, y: x if x > y else y. This lambda function compares two numbers at a time and returns the larger one. The second argument to reduce() is the list of numbers, numbers. reduce() applies the lambda function to the first two elements of the list, then applies it to the result and the next element of the list, and so on, until all list elements have been processed. The result of this process is the maximum number in the list. Finally, the maximum number is printed to the console using the print() function: print("The maximum number among the list is:", max_num). The max_num variable holds the result of the reduce() function, which is the maximum number in the list. The string "The maximum number among the list is:" is concatenated with the value of max_num using a comma as a separator. When the code is executed, it will output: The maximum number among the list is 51.
Concatenating a List of Strings using reduce():
EXAMPLE:
from functools import reduce
strings = ['Hello,', 'Hi', 'Everyone!']
concatenated_string = reduce(lambda x, y: x + ' ' + y, strings)
print(concatenated_string)
OUTPUT:
Hello, Hi Everyone!
CODE EXPLANATION:
At first, we define a list of strings and use reduce() to concatenate the strings in the list by using a lambda function that concatenates two strings with a space between them. The lambda function takes two arguments, x and y, and returns x concatenated with space and y. The reduce() function applies this lambda function to each pair of strings in the list, accumulating the result until a single string is left.