ASKSAGE: Sage Q&A Forum - RSS feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Thu, 25 Mar 2021 23:31:31 +0100Adding a 0 in variable place in a listhttps://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/ I was expecting that the command
Z=[[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
ZZ=[Z[i].insert(i,0) for i in range(len(Z))]
will insert a `0` in rank 'i' for each element of `Z` but it returns `[None, None, None, None]`.
Need help ThanksTue, 23 Feb 2021 12:19:49 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/Comment by Cyrille for <p>I was expecting that the command</p>
<pre><code>Z=[[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
ZZ=[Z[i].insert(i,0) for i in range(len(Z))]
</code></pre>
<p>will insert a <code>0</code> in rank 'i' for each element of <code>Z</code> but it returns <code>[None, None, None, None]</code>.
Need help Thanks</p>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=55835#post-id-55835I have understud. `insert` modifies directly ZTue, 23 Feb 2021 12:23:44 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=55835#post-id-55835Answer by Juanjo for <p>I was expecting that the command</p>
<pre><code>Z=[[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
ZZ=[Z[i].insert(i,0) for i in range(len(Z))]
</code></pre>
<p>will insert a <code>0</code> in rank 'i' for each element of <code>Z</code> but it returns <code>[None, None, None, None]</code>.
Need help Thanks</p>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?answer=56375#post-id-56375Another option, which is just a small modification of the original code:
sage: Z = [[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
sage: ZZ = copy(Z)
sage: [ZZ[i].insert(i,0) for i in range(len(ZZ))]; ZZ
[[0, 141, 163, 127], [107, 0, 132, 117], [85, 116, 0, 120], [121, 131, 128, 0]]
Thu, 25 Mar 2021 18:45:55 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?answer=56375#post-id-56375Comment by Juanjo for <p>Another option, which is just a small modification of the original code:</p>
<pre><code>sage: Z = [[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
sage: ZZ = copy(Z)
sage: [ZZ[i].insert(i,0) for i in range(len(ZZ))]; ZZ
[[0, 141, 163, 127], [107, 0, 132, 117], [85, 116, 0, 120], [121, 131, 128, 0]]
</code></pre>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=56379#post-id-56379Yes, you are right. Thanks for the correction.Thu, 25 Mar 2021 23:31:31 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=56379#post-id-56379Comment by slelievre for <p>Another option, which is just a small modification of the original code:</p>
<pre><code>sage: Z = [[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
sage: ZZ = copy(Z)
sage: [ZZ[i].insert(i,0) for i in range(len(ZZ))]; ZZ
[[0, 141, 163, 127], [107, 0, 132, 117], [85, 116, 0, 120], [121, 131, 128, 0]]
</code></pre>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=56378#post-id-56378Careful: use `deepcopy` instead of `copy`, otherwise `Z` ends up changed too.
Or write `ZZ = [copy(z) for z in Z]`.Thu, 25 Mar 2021 21:15:57 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=56378#post-id-56378Answer by slelievre for <p>I was expecting that the command</p>
<pre><code>Z=[[141,163,127],[107,132,117],[85,116,120],[121,131,128]]
ZZ=[Z[i].insert(i,0) for i in range(len(Z))]
</code></pre>
<p>will insert a <code>0</code> in rank 'i' for each element of <code>Z</code> but it returns <code>[None, None, None, None]</code>.
Need help Thanks</p>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?answer=55850#post-id-55850As you correctly understood, `insert` modifies a list in-place, returning `None`.
An alternative is to create new lists, e.g. using slices of the old lists.
Here is one way 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]]Tue, 23 Feb 2021 22:04:11 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?answer=55850#post-id-55850Comment by Cyrille for <p>As you correctly understood, <code>insert</code> modifies a list in-place, returning <code>None</code>.</p>
<p>An alternative is to create new lists, e.g. using slices of the old lists.</p>
<p>Here is one way to do that.</p>
<pre><code>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]]
</code></pre>
https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=55863#post-id-55863Nice. I have not thinked to this nice possibility.Wed, 24 Feb 2021 09:00:39 +0100https://ask.sagemath.org/question/55834/adding-a-0-in-variable-place-in-a-list/?comment=55863#post-id-55863