A simple list question.

Tomasz Lisowski list at notmyisp.pl
Fri Dec 23 02:36:29 EST 2005


KraftDiner wrote:
> I am trying to implement a two dimensional array.
> mylist = [[a,b,c],[d,e,f,c],[g,h,i]]
> 
> So the array is of length 3 here...
> So how do I initialize this array and then set each object?
> At some point in my code I know there will be 3 lists in the list.
> So how do I initialize this list such that I can access them
> as elements... like
> mylist[1] = [c,f,g]
> in random order...
> 
Why not just say:

mylist = [[], [], []]

since you know, that this array will have a length of 3.

Then you can safely use append. I am not quite sure, though, what you 
mean by accessing the elements in random order. What's the idea behind 
having mylist[1] = [c,f,g], when the elements c, f, and g are apread 
across all three internal lists, and at various position within these 
lists (not necessarily 1, as the mylist index suggests).

Tomasz Lisowski



More information about the Python-list mailing list