initialising a list of lists

F

Fredrik Lundh

Peter said:
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>
 
P

Peter Kleiweg

Fredrik Lundh schreef op de 16e dag van de slachtmaand van het jaar 2005:
Peter said:
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.
 
D

Daniel Dittmar

Peter said:
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
 
F

Fredrik Lundh

Peter said:
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 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>
 
S

Steven D'Aprano

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top