Skip to content

Latest commit

 

History

History
38 lines (24 loc) · 1.03 KB

reduce.md

File metadata and controls

38 lines (24 loc) · 1.03 KB

Reduce

Before reading this article, please make sure you understand how the lambda function works.

Table of Contents

Basic

reduce() recursively manipulates all the elements in a list and accumulates the results. This function is defined in functools.reduce.

General Case

reduce() takes three parameters, the first is the operation function, the second is a iterable, and the third is the initial value. The first argument of the operation function is the cumulative value and the second argument is the current element of the list.

For example, we set the initial value to 100, and then subtracted values from the list, leaving 85.

from functools import reduce

def sub(x, y):
    return x - y

reduce(sub, [1, 2, 3, 4, 5], 100)  # 85

Lambda Case

from functools import reduce

reduce(lambda x, y: x - y, [1, 2, 3, 4, 5], 100)  # 85