Ask Your Question

Revision history [back]

Use square brackets to take the term of some index in a list.

So a[n + 1] / a[n] instead of a(n + 1) / a(n).

sage: a = [6198089008491993412800,
....:     3099044504245996706400,
....:     2066029669497331137600,
....:     2324283378184497529800,
....:     1239617801698398682560]
sage: c = [a[n + 1] / a[n] for n in range(len(a) - 1)]
sage: c
[1/2, 2/3, 9/8, 8/15]

Use square brackets to take the term of some index in a list.

So a[n + 1] / a[n] instead of a(n + 1) / a(n).

sage: a = [6198089008491993412800,
....:     3099044504245996706400,
....:     2066029669497331137600,
....:     2324283378184497529800,
....:     1239617801698398682560]
sage: c = [a[n + 1] / a[n] for n in range(len(a) - 1)]
sage: c
[1/2, 2/3, 9/8, 8/15]

Added after I saw your answer using zip:

Another solution with zip would be:

sage: c = [x / y for x, y in zip(a[1:], a[:-1])]

See also the pairwise recipe in the itertools documentation, also available via the more-itertools package, and, starting with Python 3.10, directly from itertools:

Use square brackets to take the term of some index in a list.

So a[n + 1] / a[n] instead of a(n + 1) / a(n).

sage: a = [6198089008491993412800,
....:     3099044504245996706400,
....:     2066029669497331137600,
....:     2324283378184497529800,
....:     1239617801698398682560]
sage: c = [a[n + 1] / a[n] for n in range(len(a) - 1)]
sage: c
[1/2, 2/3, 9/8, 8/15]

Added after I saw your answer using zip:

Another solution with zip would be:

sage: c = [x / y for x, y in zip(a[1:], a[:-1])]

See also the pairwise recipe in the itertools documentation, also available via the more-itertools package, and, starting with Python 3.10, directly from itertools: