Skip to main content

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

If you however done this as a learning exercise, then I'd instead use iterators. I'd first change l to an iterator, via iter. Which would allow you to use next to remove the default value. After this I would then loop through the iterator and yield rather than new_list.append the new values. This can allow you to get something like:

def accumulate_sum(l):
    l = iter(l)
    try:
        total = next(l)
    except StopIteration:
        return
    yield total
    for item in l:
        total += item
        yield total

Which funnily enough is almost exactly the same to how it's done in itertools.accumulateitertools.accumulate. If you wanted to at a later date use a different function rather than addition, then you could pass that as a function, to call on each iteration.

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

If you however done this as a learning exercise, then I'd instead use iterators. I'd first change l to an iterator, via iter. Which would allow you to use next to remove the default value. After this I would then loop through the iterator and yield rather than new_list.append the new values. This can allow you to get something like:

def accumulate_sum(l):
    l = iter(l)
    try:
        total = next(l)
    except StopIteration:
        return
    yield total
    for item in l:
        total += item
        yield total

Which funnily enough is almost exactly the same to how it's done in itertools.accumulate. If you wanted to at a later date use a different function rather than addition, then you could pass that as a function, to call on each iteration.

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

If you however done this as a learning exercise, then I'd instead use iterators. I'd first change l to an iterator, via iter. Which would allow you to use next to remove the default value. After this I would then loop through the iterator and yield rather than new_list.append the new values. This can allow you to get something like:

def accumulate_sum(l):
    l = iter(l)
    try:
        total = next(l)
    except StopIteration:
        return
    yield total
    for item in l:
        total += item
        yield total

Which funnily enough is almost exactly the same to how it's done in itertools.accumulate. If you wanted to at a later date use a different function rather than addition, then you could pass that as a function, to call on each iteration.

Added more description if it's a learning experiance.
Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

If you however done this as a learning exercise, then I'd instead use iterators. I'd first change l to an iterator, via iter. Which would allow you to use next to remove the default value. After this I would then loop through the iterator and yield rather than new_list.append the new values. This can allow you to get something like:

def accumulate_sum(l):
    l = iter(l)
    try:
        total = next(l)
    except StopIteration:
        return
    yield total
    for item in l:
        total += item
        yield total

Which funnily enough is almost exactly the same to how it's done in itertools.accumulate. If you wanted to at a later date use a different function rather than addition, then you could pass that as a function, to call on each iteration.

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)

If you however done this as a learning exercise, then I'd instead use iterators. I'd first change l to an iterator, via iter. Which would allow you to use next to remove the default value. After this I would then loop through the iterator and yield rather than new_list.append the new values. This can allow you to get something like:

def accumulate_sum(l):
    l = iter(l)
    try:
        total = next(l)
    except StopIteration:
        return
    yield total
    for item in l:
        total += item
        yield total

Which funnily enough is almost exactly the same to how it's done in itertools.accumulate. If you wanted to at a later date use a different function rather than addition, then you could pass that as a function, to call on each iteration.

Source Link
Peilonrayz
  • 44.6k
  • 7
  • 80
  • 158

In Python 3 itertools gained a new function accumulate that does what you want. I'd recommend that you instead used this function. Or if you can't if you're in Python 2 to upgrade. This would single handedly change your code to:

from itertools import accumulate

new_l = accumulate(l)