Ask Your Question

Revision history [back]

Sage <= 8.9 was based on Python 2 in which zip would return lists.

Sage >= 9.0 is based on Python 3 in which zip returns "zip objects".

Zip objects are iterable, so list can turn them into lists.

Example (from another Ask Sage question):

sage: a = [250, 770, 360, 190, 230, -1, 0, 0, 0, 0, 0, 1, 31,
....:      44, 14, 27, 3, 0, 480, 1770, 800, 580, 160, 0, 1,
....:      0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0,
....:      0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1]
sage: b = list(zip(*[iter(a)]*6))
sage: b
[(250, 770, 360, 190, 230, -1),
 (0, 0, 0, 0, 0, 1),
 (31, 44, 14, 27, 3, 0),
 (480, 1770, 800, 580, 160, 0),
 (1, 0, 0, 0, 0, 0),
 (0, 1, 0, 0, 0, 0),
 (0, 0, 1, 0, 0, 0),
 (0, 0, 0, 1, 0, 0),
 (0, 0, 0, 0, 1, 0),
 (0, 0, 0, 0, 0, 1)]

Here we could also use matrix:

sage: m = matrix(10, 6, a)
sage: m
[ 250  770  360  190  230   -1]
[   0    0    0    0    0    1]
[  31   44   14   27    3    0]
[ 480 1770  800  580  160    0]
[   1    0    0    0    0    0]
[   0    1    0    0    0    0]
[   0    0    1    0    0    0]
[   0    0    0    1    0    0]
[   0    0    0    0    1    0]
[   0    0    0    0    0    1]

Then we can get a list of rows:

sage: m.rows()
[(250, 770, 360, 190, 230, -1),
 (0, 0, 0, 0, 0, 1),
 (31, 44, 14, 27, 3, 0),
 (480, 1770, 800, 580, 160, 0),
 (1, 0, 0, 0, 0, 0),
 (0, 1, 0, 0, 0, 0),
 (0, 0, 1, 0, 0, 0),
 (0, 0, 0, 1, 0, 0),
 (0, 0, 0, 0, 1, 0),
 (0, 0, 0, 0, 0, 1)]

The rows are really vectors (which display exactly as tuples).

To get actual tuples:

sage: [tuple(row) for row in m]
[(250, 770, 360, 190, 230, -1),
 (0, 0, 0, 0, 0, 1),
 (31, 44, 14, 27, 3, 0),
 (480, 1770, 800, 580, 160, 0),
 (1, 0, 0, 0, 0, 0),
 (0, 1, 0, 0, 0, 0),
 (0, 0, 1, 0, 0, 0),
 (0, 0, 0, 1, 0, 0),
 (0, 0, 0, 0, 1, 0),
 (0, 0, 0, 0, 0, 1)]