Hello,

>>> b = [[] for _ in xrange(6)] # <- note the xrange!
>>> b[3].append('X')
>>> b
[[], [], [], ['X'], [], []]

This syntax might be less hairy but could be better when you use large table.

You can hide this syntax by making a function:

def buildMatrix(nbRows):
    return [[] for _ in xrange(nbRows)]

Then you call:

>>> b = buildMatrix(6)

Or:

>>> b = buildMatrix(nbRows=6)

Is it better for you?

Cyril


On 11/16/05, Peter Kleiweg <[EMAIL PROTECTED]> wrote:
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:

> Peter Kleiweg wrote:
>
> > This does not what I want it to do:
> >
> >    >>> a = [[]] * 6
> >    >>> a[3].append('X')
> >    >>> a
> >    [['X'], ['X'], ['X'], ['X'], ['X'], ['X']]
> >
> > This does what I want:
> >
> >    >>> b = [[] for _ in range(6)]
> >    >>> b[3].append('X')
> >    >>> b
> >    [[], [], [], ['X'], [], []]
> >
> > The first is clear and wrong. The second is hairy and right.
> > Is there a way to do it clear and right?
>
> http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list

In other words: no there isn't.

--
Peter Kleiweg  L:NL,af,da,de,en,ia,nds,no,sv,(fr,it)  S:NL,de,en,(da,ia)
info: http://www.let.rug.nl/~kleiweg/ls.html
--
http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to