Skip to main content
deleted 22 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This is my first time posting! But sinceSince you asked for a Pythonic solution and I see none so far, I propose:

new_L = [sum(L[:i+1]) for i in range(len(L))] 

It's certainly less efficient than an accumulator -- it's O((n^2)/2)\$O(\frac{n^2}{2})\$ vs O(n)\$O(n)\$ -- but it uses a list comprehension as you suggested.

This is my first time posting! But since you asked for a Pythonic solution and I see none so far, I propose:

new_L = [sum(L[:i+1]) for i in range(len(L))] 

It's certainly less efficient than an accumulator -- it's O((n^2)/2) vs O(n) -- but it uses a list comprehension as you suggested.

Since you asked for a Pythonic solution and I see none so far, I propose:

new_L = [sum(L[:i+1]) for i in range(len(L))] 

It's certainly less efficient than an accumulator -- it's \$O(\frac{n^2}{2})\$ vs \$O(n)\$ -- but it uses a list comprehension as you suggested.

Source Link

This is my first time posting! But since you asked for a Pythonic solution and I see none so far, I propose:

new_L = [sum(L[:i+1]) for i in range(len(L))] 

It's certainly less efficient than an accumulator -- it's O((n^2)/2) vs O(n) -- but it uses a list comprehension as you suggested.