Ask Your Question

creating list n many times

asked 2015-10-12 15:45:30 +0200

GA316 gravatar image

How to define n many empty lists where n is the number of vertices of the given arbitrary graph.

Thanks in advance.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2015-10-12 15:52:37 +0200

B r u n o gravatar image

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]
edit flag offensive delete link more


thanks. is this like two dimensional list?

GA316 gravatar imageGA316 ( 2015-10-12 15:56:40 +0200 )edit

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].

B r u n o gravatar imageB r u n o ( 2015-10-12 18:33:44 +0200 )edit

Thanks a lot Bruno.

GA316 gravatar imageGA316 ( 2015-10-13 06:37:32 +0200 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2015-10-12 15:45:30 +0200

Seen: 830 times

Last updated: Oct 12 '15