Home >Backend Development >Python Tutorial >What is reduce in python
reduce()
The function accumulates elements in the parameter sequence.
The function performs the following operations on all data in a data set (linked list, tuple, etc.): Use the function function (with two parameters) passed to reduce to first , operate on 2 elements, and the result obtained is then operated with the third data using the function function, and finally a result is obtained.
reduce() function receives three parameters, as shown below
reduce() function syntax:
reduce(function, iterable[, initializer])
The working process of reduce is: in the process of iterating sequence (tuple, list, dictionary, string and other iterable objects), first pass the first two elements to the function parameters. After the function processes, then The obtained result and the third element are passed as two parameters to the function parameter, and the result obtained after function processing and the fourth element are passed as two parameters to the function parameter, and so on. If the initial value is passed in, then the first and second elements of the sequence are not passed first, but the initial value and the first element. After such cumulative calculations, the sequence is merged into a single return value
For example
Look at another example
Related learning recommendations: python tutorial
The above is the detailed content of What is reduce in python. For more information, please follow other related articles on the PHP Chinese website!