Re: initialising a list of lists

2005-11-16 Thread Steven D'Aprano
On Wed, 16 Nov 2005 13:58:45 +0100, 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].app

Re: initialising a list of lists

2005-11-16 Thread Fredrik Lundh
Peter Kleiweg wrote: >> http://www.python.org/doc/faq/programming.html#how-do-i-create-a-multidimensional-list > > In other words: no there isn't. For people who actually knows Python, a list comprehension is clear and obviously correct. For people who actually knows Python, your first solution

Re: initialising a list of lists

2005-11-16 Thread Daniel Dittmar
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 > [[], [], [],

Re: initialising a list of lists

2005-11-16 Thread Cyril Bazin
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

Re: initialising a list of lists

2005-11-16 Thread Peter Kleiweg
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: > > > >

Re: initialising a list of lists

2005-11-16 Thread Fredrik Lundh
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'], [],

initialising a list of lists

2005-11-16 Thread Peter Kleiweg
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.