1 | initial version |
The method append
modifies the list in place.
It does not return anything (or equivalently, it returns None
).
It has the side-effect of modifying the list.
Doing d = c.append(0)
assigns the result of c.append(0)
(i.e., None
) to d
.
To get a new list consisting of the former value of c
with one extra element 0
, use:
sage: d = c + [0]
2 | No.2 Revision |
The method append
modifies the list in place.
It does not return anything (or equivalently, it returns None
).
It has the side-effect of modifying the list.
Doing d = c.append(0)
assigns the result of c.append(0)
(i.e., None
) to d
.
To get a new list consisting of the former value of c
with one extra element 0
, use:
sage: d = c + [0]
Recommended: go through a few of SageMath's thematic tutorials, especially those in the section "Introduction to Python".
3 | No.3 Revision |
The method append
modifies the list in place.
It does not return anything (or equivalently, it returns None
).
It has the side-effect of modifying the list.
Doing d = c.append(0)
assigns the result of c.append(0)
(i.e., None
) to d
.
To get a new list consisting of the former value of c
with one extra element 0
, use:
sage: d = c + [0]
Recommended: go through a few of SageMath's thematic tutorials, especially those in the section "Introduction to Python".
In a comment after my initial answer, there is a follow up question about Python equivalents of some Mathematica functions for operating on lists: RotateLeft, RotateRight, PadLeft, PadRight (the links point to the Wolfram language reference manual entries for those functions).
Implementing their equivalent in Sage is a good exercise after going through the tutorial.
One could make two versions, one that rotates or pads inplace, one that returns the result.
Here is one way to rotate right one step, inplace:
sage: a = [0, 3, 9, 5, 6, 8, 4]
sage: a[:0] = [a.pop()]
sage: a
[4, 0, 3, 9, 5, 6, 8]
Here is a rotated_right
function:
def rotated_right(a, n=1):
if len(a) < 2 or n % len(a) == 0:
return a[:]
n = n % len(a)
return a[-n:] + a[:-n]
A pad_right
function and a padded_right
function:
def pad_right(a, n=1)
if len(a) < n:
a.extend([0] * (n - len(a)))
def padded_right(a, n=1)
if len(a) < n:
return a[:] + [0] * (n - len(a))
return a[:]
Further recommendations: