# Prefix sum array in Python using accumulate function

We are given an array, find prefix sums of given array.

Examples:

Input : arr = [1, 2, 3] Output : sum = [1, 3, 6] Input : arr = [4, 6, 12] Output : sum = [4, 10, 22]

A **prefix sum** is a sequence of partial sums of a given sequence. For example, the cumulative sums of the sequence {a, b, c, …} are a, a+b, a+b+c and so on. We can solve this problem in python quickly using **accumulate(iterable)** method.

`# function to find cumulative sum of array ` `from` `itertools ` `import` `accumulate ` ` ` `def` `cumulativeSum(` `input` `): ` ` ` `print` `(` `"Sum :"` `, ` `list` `(accumulate(` `input` `))) ` ` ` `# Driver program ` `if` `__name__ ` `=` `=` `"__main__"` `: ` ` ` `input` `=` `[` `4` `, ` `6` `, ` `12` `] ` ` ` `cumulativeSum(` `input` `) ` |

*chevron_right*

*filter_none*

Output:

Sum = [4, 10, 22]

## Recommended Posts:

- Rearrange the array to maximize the number of primes in prefix sum of the array
- Count the number of primes in the prefix sum array of the given array
- Python | Prefix sum list
- Prefix Sum of Matrix (Or 2D Array)
- Python | Get the numeric prefix of given string
- Python | Get numeric prefix of given string
- Python | Prefix key match in dictionary
- Longest prefix that contains same number of X and Y in an array
- Python | Prefix extraction before specific character
- Python | Prefix Sum Subarray till False value
- Prefix matching in Python using pytrie module
- Sum 2D array in Python using map() function
- Python | Split strings in list with same prefix in all elements
- Python | Ways to determine common prefix in set of strings
- Python program to print the substrings that are prefix of the given string

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.