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.
How to define n many empty lists where n is the number of vertices of the given arbitrary graph.
Thanks in advance.
If 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]
thanks. is this like two dimensional list?
I 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]
.
Thanks a lot Bruno.
Asked: 9 years ago
Seen: 1,067 times
Last updated: Oct 12 '15