Interview Kickstart has enabled over 21000 engineers to uplevel.
Python supports object-oriented programming and has a concise, readable, and easy-to-learn syntax. It is no wonder that it is one of the most popular programming languages. An integral part of Python are its built-in functions.
We've written a series of articles to help you learn and brush up on the most useful Python functions. In this article, we’ll learn about Python's reduce() function and how to use it.
Note that the function reduce() in Python was originally built-in, and still is, in 2.x versions. But in versions starting 3.0, it has been moved to functools due to readability and performance-related factors. We’ll learn more about reduce() and how to use it in this article.
If you are preparing for a tech interview, check out our technical interview checklist, interview questions page, and salary negotiation e-book to get interview-ready! Also, read Python String join() Method, Python Exit commands, and Type and Isinstance In Python for more content on Python coding interview preparation.
Having trained over 10,000 software engineers, we know what it takes to crack the toughest tech interviews. Our alums consistently land offers from FAANG+ companies. The highest ever offer received by an IK alum is a whopping $1.267 Million!
At IK, you get the unique opportunity to learn from expert instructors who are hiring managers and tech leads at Google, Facebook, Apple, and other top Silicon Valley tech companies.
Want to nail your next tech interview? Sign up for our FREE Webinar.
This article will cover:
The reduce() function in Python implements a mathematical technique called folding or reduction. This technique involves reducing a list of items to a single cumulative value. Python’s reduce() operates on any iterable. Also, for using reduce() in Python 3.x, reduce() needs to first be imported using an import statement to the current scope.
Python’s reduce() function takes an existing function and applies the function cumulatively to every item in the given iterable, and gives a single return value. Here’s how it works:
Let us now look at the syntax for reduce().
Syntax of reduce() In Python 3.x
functools.reduce(function, iterable[, initializer])
Syntax of reduce() In Python 2.x and Earlier Versions
reduce(function, iterable[, initializer])
Here, we take a look at how you can use the Python function reduce() in Python 3.x next time you need it:
# Python 3.x code to demonstrate the working of the reduce() function
# Importing functools to use reduce()
import functools
#Defining function which will be used with reduce to find sum
def sum(num1,num2):
print("num1= ", num1, " num2= ", num2)
return num1+num2
# Using reduce with the above sum function to find the sum of listExample
print("The sum of numbers from 1 to 5, i.e., in the range [1,6), is: ", functools.reduce(sum, range(1,6)))
# Creating a list
listExample = [1, 2, 3, 4, 5]
# Using reduce with lambda to find the sum of listExample
print("The sum of all the elements in the list is: ", end="")
print(functools.reduce(lambda num1, num2: num1+num2, listExample))
# Using reduce to find the product of listExample
print("The product of all the elements in the list is: ", end="")
print(functools.reduce(lambda num1, num2: num1*num2, listExample))
# Using reduce to find the maximum element from listExample
print("The maximum element of the list is: ", end="")
print(functools.reduce(lambda num1, num2: num1 if num1 > num2 else num2, listExample))
num1= 1 num2= 2
num1= 3 num2= 3
num1= 6 num2= 4
num1= 10 num2= 5
The sum of numbers from 1 to 5, i.e., in the range [1,6), is: 15
The sum of all the elements in the list is: 15
The product of all the elements in the list is: 120
The maximum element of the list is: 5
For Python 2.x, we can directly use reduce() without needing to import functools as can be seen in the example below:
# Python 2.x code to demonstrate the working of the reduce() function
#Defining function which will be used with reduce to find sum
def sum(num1,num2):
print("num1= ", num1, " num2= ", num2)
return num1+num2
# Using reduce with the above sum function to find the sum of listExample
print("The sum of numbers from 1 to 5, i.e., in the range [1,6) is: ", reduce(sum, range(1,6)))
# Creating a list
listExample = [1, 2, 3, 4, 5]
# Using reduce with lambda to find the sum of listExample
print("The sum of all the elements in the list is: ")
print(reduce(lambda num1, num2: num1+num2, listExample))
# Using reduce to find the product of listExample
print("The product of all the elements in the list is: ")
print(reduce(lambda num1, num2: num1*num2, listExample))
# Using reduce to find the maximum element from listExample
print("The maximum element of the list is : ")
print(reduce(lambda num1, num2: num1 if num1 > num2 else num2, listExample))
('num1= ', 1, ' num2= ', 2)
('num1= ', 3, ' num2= ', 3)
('num1= ', 6, ' num2= ', 4)
('num1= ', 10, ' num2= ', 5)
('The sum of numbers from 1 to 5, i.e., in the range [1,6) is: ', 15)
The sum of all the elements in the list is:
15
The product of all the elements in the list is:
120
The maximum element of the list is :
5
Found this article helpful? You can learn about more Python functions in our learn folder.
Q1. What is reduce() in Python?
The reduce() function in Python implements a mathematical technique called folding or reduction and works by applying a function to an iterable and reducing it to a single cumulative value.
Q2. What does reduce function return in Python?
Facilitating a functional approach in Python, reduce() takes a function and an iterable as arguments and returns the final computed cumulative value.
Q3. Where in Python is reduce() located?
Starting Python 3.x, the reduce() function is defined and located in the functools module but is a built-in function in earlier versions like Python 2.x.
Q4. Is reduce a built-in function?
Python's reduce() function was originally a built-in function (and still is in Python 2. x), but it was moved to functools.
Q5. What arguments does the reduce() function take in Python?
The reduce() function takes two arguments in Python — function and an iterable.
Whether you’re a coding engineer gunning for a software developer or software engineer role, a tech lead, or you’re targeting management positions at top companies, IK offers courses specifically designed for your needs to help you with your technical interview preparation!
If you’re looking for guidance and help with getting started, sign up for our FREE webinar. As pioneers in the field of technical interview preparation, we have trained thousands of software engineers to crack the most challenging coding interviews and land jobs at their dream companies, such as Google, Facebook, Apple, Netflix, Amazon, and more!
Attend our webinar on
"How to nail your next tech interview" and learn