Ask Your Question

Revision history [back]

As you correctly understood, insert modifies a list in-place, and returns None.

Instead of modifying the lists in place, one could create new lists.

One way would be to make the new list of lists using slices of the old lists.

Here is one way that could be done.

sage: Z = [[141, 163, 127], [107, 132, 117], [85, 116, 120], [121, 131, 128]]
sage: ZZ = [z[:i] + [0] + z[i:] for i, z in enumerate(Z)]
sage: ZZ
[[0, 141, 163, 127], [107, 0, 132, 117], [85, 116, 0, 120], [121, 131, 128, 0]]

As you correctly understood, insert modifies a list in-place, and returns returning None.

Instead of modifying the lists in place, one could An alternative is to create new lists.

One way would be to make the new list of lists lists, e.g. using slices of the old lists.

Here is one way that could be done.to do that.

sage: Z = [[141, 163, 127], [107, 132, 117], [85, 116, 120], [121, 131, 128]]
sage: ZZ = [z[:i] + [0] + z[i:] for i, z in enumerate(Z)]
sage: ZZ
[[0, 141, 163, 127], [107, 0, 132, 117], [85, 116, 0, 120], [121, 131, 128, 0]]