Sling Academy
Home/Python/Python reduce() function: Tutorial & examples

Python reduce() function: Tutorial & examples

Last updated: June 16, 2023

Overview

The reduce() function in Python is part of the functools module and is used to perform a specified function on a sequence of elements, reducing it to a single value. It takes two parameters: the function to be applied and the iterable sequence to be reduced.

Syntax:

reduce(function, iterable)

Parameters:

  • function: The function to be applied. It should take two arguments and return a single value. The function is applied cumulatively to the items of the iterable from left to right.
  • iterable: The sequence of elements to be reduced.

In general, the reduce() function is mostly used when working with lists.

Examples

Some examples of using the reduce() function in practice, in order from basic to advanced.

Summing a list of numbers

The reduce() function can help us easily calculate the sum of all numbers in a list:

from functools import reduce

numbers = [2023, 2024, 2025, 2026, 2027]

sum = reduce(lambda x, y: x + y, numbers)
print(sum) # 10125

Flattening a list of lists

You can use the reduce() function to flatten a list of lists into a single list with only a single line of code:

from functools import reduce

nested_list = [[1, 2], [3, 4], [5, 6]]

flat_list = reduce(lambda x, y: x + y, nested_list)
print(flat_list)  # Output: [1, 2, 3, 4, 5, 6]

Finding the longest string

Let’s say we have a list of strings. What we need to do is to find the longest string from there. The reduce() function can help us get the job done without spending too much effort:

from functools import reduce

strings = ["dog", "slingacademy.com","turtle", "dark hole", "banana"]

longest = reduce(lambda x, y: x if len(x) > len(y) else y, strings)
print(longest)  # slingacademy.com

Finding the minimum and maximum values

Here’s the code:

from functools import reduce

numbers = [12, 45, 23, 67, 9]

minimum = reduce(lambda x, y: x if x < y else y, numbers)
print(minimum)  # Output: 9

maximum = reduce(lambda x, y: x if x > y else y, numbers)
print(maximum)  # Output: 67

That’s it. The tutorial ends here. Happy coding & have a nice day!

Next Article: Python: 2 Ways to Find Common Elements in 2 Lists

Previous Article: Filtering Lists in Python (4 Examples)

Series: Python List Tutorials (Basic and Advanced)

Python

You May Also Like

  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots
  • Monitoring Order Book Imbalances for Trading Signals via cryptofeed
  • Detecting Arbitrage Opportunities Across Exchanges with cryptofeed