473,472 Members | 1,748 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

initialising a list of lists


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
Nov 22 '05 #1
10 1958
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/progra...mensional-list

</F>

Nov 22 '05 #2
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/progra...mensional-list

</F>

Nov 22 '05 #3
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/progra...mensional-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
Nov 22 '05 #4
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/progra...mensional-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
Nov 22 '05 #5
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

Nov 22 '05 #6
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

Nov 22 '05 #7
Peter Kleiweg wrote:
http://www.python.org/doc/faq/progra...mensional-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 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>

Nov 22 '05 #8
Peter Kleiweg wrote:
http://www.python.org/doc/faq/progra...mensional-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 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>

Nov 22 '05 #9
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.

Nov 22 '05 #10
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.

Nov 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Glen Able | last post by:
Hi, I have a collection of lists, something like this: std::list<Obj*> lists; Now I add an Obj to one of the lists: Obj* gary; lists.push_front(gary);
1
by: Booser | last post by:
// Merge sort using circular linked list // By Jason Hall <booser108@yahoo.com> #include <stdio.h> #include <stdlib.h> #include <time.h> #include <math.h> //#define debug
2
by: Cox | last post by:
Hello: My address jsmith435@cox.net is subscribed to at least the PHP General mailing list. I have for days now been trying to unsubscribe from all PHP mail lists. I have followed the...
16
by: Michael M. | last post by:
How to find the longst element list of lists? I think, there should be an easier way then this: s1 = s2 = s3 = if len(s1) >= len(s2) and len(s1) >= len(s3): sx1=s1 ## s1 ist längster
19
by: Dongsheng Ruan | last post by:
with a cell class like this: #!/usr/bin/python import sys class Cell: def __init__( self, data, next=None ): self.data = data
107
by: DaveC | last post by:
I always used to initialise variables at declaration, then a couple of colleagues started telling me it was bad practice and that the compiler should be left to spot the use of uninitilised...
2
by: mark4asp | last post by:
Q: Initialising and updating a class with only static members & database dependency I have a class with the following members: public static List<ACISACIS_List; static AssetClass() { //...
10
by: AZRebelCowgirl73 | last post by:
This is what I have so far: My program! import java.util.*; import java.lang.*; import java.io.*; import ch06.lists.*; public class UIandDB {
36
by: pereges | last post by:
Hi, I am wondering which of the two data structures (link list or array) would be better in my situation. I have to create a list of rays for my ray tracing program. the data structure of ray...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.