DevOps Articles

Curated articles, resources, tips and trends from the DevOps World.

Reduce() vs Accumulate() in Python

4 years ago codeburst.io
Reduce() vs Accumulate() in Python

Summary: This is a summary of an article originally published by the source. Read the full original article here →

The functools module is for higher-order functions. Apply function of two arguments cumulatively to the items of iterable, from left to right, so as to reduce the iterable to a single value.

If the optionalinitializer is present, it is placed before the items of the iterable in the calculation, and serves as a default when the iterable is empty.

Example Iterable contains only one item, reduce() will return that item.Example If iterable is empty and the initializer is given, reduce() will return the initializer.

Example 3:Function argument is given as max(), to find a running maximumExample 4: If the initial value is mentioned, it will start accumulating from the initial value.Example 5: If the iterable is empty and the initial parameter is mentioned, it will return the initial value.Example 6: If iterable contains one element and the initial parameter is not mentioned, it will return that element.Example Iterating through the iterator using for loop

Made with pure grit © 2024 Jetpack Labs Inc. All rights reserved. www.jetpacklabs.com