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.Tue, 13 Oct 2015 06:37:32 +0200creating list n many timeshttps://ask.sagemath.org/question/29935/creating-list-n-many-times/ How to define n many empty lists where n is the number of vertices of the given arbitrary graph.
Thanks in advance.Mon, 12 Oct 2015 15:45:30 +0200https://ask.sagemath.org/question/29935/creating-list-n-many-times/Answer by B r u n o for <p>How to define n many empty lists where n is the number of vertices of the given arbitrary graph.</p>
<p>Thanks in advance.</p>
https://ask.sagemath.org/question/29935/creating-list-n-many-times/?answer=29936#post-id-29936If `n` is your number of vertices, you can for instance create a list of `n` empty lists as follows:
sage: L = [[] for _ in range(n)]
And then you can access the (empty) list number `i` (for `i` from `0` to `n-1`) using:
sage: L[i]
[]
sage: L[i].append(0)
sage: L[i]
[0]Mon, 12 Oct 2015 15:52:37 +0200https://ask.sagemath.org/question/29935/creating-list-n-many-times/?answer=29936#post-id-29936Comment by GA316 for <p>If <code>n</code> is your number of vertices, you can for instance create a list of <code>n</code> empty lists as follows:</p>
<pre><code>sage: L = [[] for _ in range(n)]
</code></pre>
<p>And then you can access the (empty) list number <code>i</code> (for <code>i</code> from <code>0</code> to <code>n-1</code>) using:</p>
<pre><code>sage: L[i]
[]
sage: L[i].append(0)
sage: L[i]
[0]
</code></pre>
https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29937#post-id-29937thanks. is this like two dimensional list?Mon, 12 Oct 2015 15:56:40 +0200https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29937#post-id-29937Comment by B r u n o for <p>If <code>n</code> is your number of vertices, you can for instance create a list of <code>n</code> empty lists as follows:</p>
<pre><code>sage: L = [[] for _ in range(n)]
</code></pre>
<p>And then you can access the (empty) list number <code>i</code> (for <code>i</code> from <code>0</code> to <code>n-1</code>) using:</p>
<pre><code>sage: L[i]
[]
sage: L[i].append(0)
sage: L[i]
[0]
</code></pre>
https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29941#post-id-29941I don't know what you mean by "two dimensional list". As defined in my answer, `L` is a "list of list", so in some sense can be thought of as "two dimensional". To access the third element of the second list in `L`, you would for instance write `L[1][2]`.Mon, 12 Oct 2015 18:33:44 +0200https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29941#post-id-29941Comment by GA316 for <p>If <code>n</code> is your number of vertices, you can for instance create a list of <code>n</code> empty lists as follows:</p>
<pre><code>sage: L = [[] for _ in range(n)]
</code></pre>
<p>And then you can access the (empty) list number <code>i</code> (for <code>i</code> from <code>0</code> to <code>n-1</code>) using:</p>
<pre><code>sage: L[i]
[]
sage: L[i].append(0)
sage: L[i]
[0]
</code></pre>
https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29955#post-id-29955Thanks a lot Bruno.Tue, 13 Oct 2015 06:37:32 +0200https://ask.sagemath.org/question/29935/creating-list-n-many-times/?comment=29955#post-id-29955