Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

Python Dictionary

  1. Home
  2. Python Dictionary
  3. functools.reduce() / functools.partial()

functools.reduce() / functools.partial()

functools.reduce() applies a two-argument function cumulatively to the elements of an iterable from left to right, reducing it to a single value. functools.partial() creates a new function with some arguments of an existing function pre-filled (a partially applied function). Both are useful for writing code in a functional programming style.

Syntax

from functools import reduce, partial

# Reduction (apply function from left to right)
reduce(function, iterable, initializer=None)

# Partial application (create a new function with fixed arguments)
partial(func, *args, **kwargs)

Functions

FunctionDescription
reduce(func, iterable)Applies func cumulatively to the elements of the iterable from left to right and returns a single value.
reduce(func, iterable, init)Starts the reduction with init as the initial value. Does not raise an error if the iterable is empty.
partial(func, *args, **kwargs)Returns a new function object with some arguments of func pre-filled.

Sample Code

from functools import reduce, partial
import operator

# reduce: sum all elements in a list (same result as sum())
nums = [1, 2, 3, 4, 5]
total = reduce(lambda a, b: a + b, nums)
print(total)    # 15

# reduce: combine with the operator module
product = reduce(operator.mul, nums)
print(product)  # 120 (1×2×3×4×5)

# reduce: specify an initial value
total2 = reduce(lambda a, b: a + b, nums, 100)
print(total2)   # 115 (100 + 15)

# reduce: find the maximum value (same result as max())
max_val = reduce(lambda a, b: a if a > b else b, [3, 1, 4, 1, 5, 9, 2, 6])
print(max_val)  # 9

# reduce: flatten a nested list
nested = [[1, 2], [3, 4], [5, 6]]
flat = reduce(lambda a, b: a + b, nested)
print(flat)     # [1, 2, 3, 4, 5, 6]

# partial: pre-fill some arguments of a function
def power(base, exp):
    return base ** exp

square = partial(power, exp=2)  # fix exp to 2
cube   = partial(power, exp=3)  # fix exp to 3

print(square(5))    # 25
print(cube(3))      # 27

# partial: fix the separator for print
print_tsv = partial(print, sep='\t')
print_tsv('Name', 'Age', 'Department')  # Name\tAge\tDepartment

# partial: fix the first argument
add_10 = partial(lambda a, b: a + b, 10)
print(add_10(5))    # 15
print(add_10(20))   # 30

Notes

reduce() was a built-in function in Python 2, but was moved to the functools module in Python 3. In many cases it can be replaced with built-in functions such as sum(), max(), min(), any(), and all(), but reduce() is handy when you want to express more complex aggregation logic in a single line. If the iterable is empty and no initializer is provided, a TypeError is raised.

partial() is useful when you need to pass a function as a callback with certain arguments already set, or when you want to reuse the same function with slightly different configurations. You can achieve the same result with a lambda expression, but using partial() makes the intent clearer.

If you find any errors or copyright issues, please .