Ask Your Question

Revision history [back]

Do you just want syntax to add the first k entries in a list? As @kcrisman alluded, you can get the first k entries by slicing, and then add them with

add(a[:k+1])

So you could use it like this:

sage: a = [n for n in range(5)]   # a is a list
sage: a
[0, 1, 2, 3, 4]
sage: seq = [add(a[:k+1]) for k in range(len(a))]
sage: seq
[0, 1, 3, 6, 10]

This works as long as the entries of your list can be added. Here's an example with symbolic variables:

sage: a = [var('a'+str(n)) for n in range(5)]
sage: a
[a0, a1, a2, a3, a4]
sage: seq = [add(a[:k+1]) for k in range(len(a))]
sage: seq
[a0, a0 + a1, a0 + a1 + a2, a0 + a1 + a2 + a3, a0 + a1 + a2 + a3 + a4]