Is this function built in in Python 2.7?
Question: Suppose I have two lists of the same size in Python, the first:

    
    
    [100, 200, 300, 400]
    

and I want the other to be:

    
    
    [0, 100, 300, 600] 
    

which is each element in the 2nd list equals the sum of all previous elements
in the first.

Is there a built-in Python function that does such an operation on a list? Or
do I have to think about an algorithm to do it?


Answer: If you use Python 3.2+, you can use
[`itertools.accumulate`](https://docs.python.org/3/library/itertools.html#itertools.accumulate):

    
    
    >>> import itertools
    >>>
    >>> a_list = [100, 200, 300, 400]
    >>> list(itertools.accumulate([0] + a_list[:-1]))
    [0, 100, 300, 600]
    

**UPDATE**

To avoid creation of temporary list, use
[`itertools.islice`](https://docs.python.org/3/library/itertools.html#itertools.islice),
[`itertools.chain`](https://docs.python.org/3/library/itertools.html#itertools.chain):

    
    
    >>> from itertools import chain, accumulate, islice
    >>>
    >>> a_list = [100, 200, 300, 400]
    >>> list(accumulate(chain([0], islice(a_list, 0, len(a_list) - 1))))
    [0, 100, 300, 600]
    

