initialising a list of lists

3 views
Skip to first unread message

Peter Kleiweg

unread,
Nov 16, 2005, 7:58:45 AM11/16/05
to

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?

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

Fredrik Lundh

unread,
Nov 16, 2005, 8:13:34 AM11/16/05
to pytho...@python.org
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

</F>

Peter Kleiweg

unread,
Nov 16, 2005, 8:27:30 AM11/16/05
to
Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:

In other words: no there isn't.

Daniel Dittmar

unread,
Nov 16, 2005, 9:08:50 AM11/16/05
to
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

Define a function:

import copy

def init_list (count, element):
return [copy.copy (element) for i in xrange (count)]

> and right?

Test it.

Daniel

Fredrik Lundh

unread,
Nov 16, 2005, 11:27:43 AM11/16/05
to pytho...@python.org
Peter Kleiweg wrote:

For people who actually knows Python, a list comprehension is clear and
obviously correct.

For people who actually knows Python, your first solution is also obviously
wrong. To create a new list objects, you have to execute the list display.
New objects never appear out of the blue, and Python hardly ever copies
objects unless you tell it to do so.

</F>

Steven D'Aprano

unread,
Nov 16, 2005, 4:18:08 PM11/16/05
to
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].append('X')
> >>> b
> [[], [], [], ['X'], [], []]
>
> The first is clear and wrong.

That is correct. It is wrong because you make six references to the same
empty list instead of six different empty lists.

> The second is hairy and right.

I disagree. I think the second method is just as clear as the first.

> Is there a way to do it clear and right?

There are lots of ways to do it right. Clarity is in the eye of the
beholder. But perhaps the clearest way is the most explicit:

>>> c = []
>>> for i in range(6):
... c.append([])
>>> c[3].append('X')
>>> c


[[], [], [], ['X'], [], []]


I can't help feeling though that this is such a common task, and so often
trips up newbies, that it deserves a built in list method. I base my
reasoning on the existence of methods like extend:

Instead of writing:

for item in seq:
L.append(item)

the Powers That Be created L.extend(seq). This isn't the only case of very
simple idioms being made even shorter in Python.

So perhaps there should be a list method that takes an integer argument
and appends that many empty lists:

d = []
d.append_empties(5)

Or even a function that does this:

def nested(numcopies, base=None, *args):
if base is None:
base = []
for i in range(numcopies):
base.append(args[:])
return base

--
Steven.

Reply all
Reply to author
Forward
0 new messages