473,472 Members | 2,139 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multi-dimensional list initialization trouble

Hello I found this very strange; is it a bug, is it a "feature", am I
being naughty or what?
foo = [[0, 0], [0, 0]]
baz = [ [0]*2 ] * 2
foo [[0, 0], [0, 0]] baz [[0, 0], [0, 0]] foo[0][0]=1
baz[0][0]=1
foo [[1, 0], [0, 0]] baz

[[1, 0], [1, 0]]

Why on earth does foo and baz behave differently??

Btw.:
Python 2.4.1 (#1, Apr 10 2005, 22:30:36)
[GCC 3.3.5] on linux2

--- Jon Øyvind

May 25 '06 #1
3 1008
jo****@gmail.com wrote:
Hello I found this very strange; is it a bug, is it a "feature", am I
being naughty or what?
foo = [[0, 0], [0, 0]]
baz = [ [0]*2 ] * 2...
Why on earth does foo and baz behave differently??


This is a frequently made mistake.
try also:
bumble = [[0]*2 for 0 in xrange(2)]
Think hard about why that might be.
Then try:
[id(x) for x in foo]
[id(x) for x in baz]
[id(x) for x in bumble]


Now check your upper scalp for lightbulbs.

--Scott David Daniels
sc***********@acm.org
May 25 '06 #2
jo****@gmail.com wrote:
Hello I found this very strange; is it a bug, is it a "feature", am I
being naughty or what?


the repeat operator (*) creates a new list with references to the same
inner objects, so you end up with a list containing multiple references
to the same list. also see:

http://pyfaq.infogami.com/how-do-i-c...mensional-list

</F>

May 25 '06 #3
An expression like this creates a list of integers:
[0] * 2 [0, 0]

But an expression like this creates list of references to the list
named `foo': foo = [0, 0]
baz = [foo] * 2 [foo, foo]

So, setting baz[0][0] = 1, is really setting foo[0] = 1. There is only
one instance of foo, but you have multiple references.

Try a list comprehension to get the result you want: foo = [[0 for ii in range(2)] for jj in range(2)]


May 25 '06 #4

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

Similar topics

37
by: ajikoe | last post by:
Hello, Is anyone has experiance in running python code to run multi thread parallel in multi processor. Is it possible ? Can python manage which cpu shoud do every thread? Sincerely Yours,...
4
by: Frank Jona | last post by:
Intellisense with C# and a multi-file assembly is not working. With VB.NET it is working. Is there a fix availible? We're using VisualStudio 2003 Regards Frank
12
by: * ProteanThread * | last post by:
but depends upon the clique: ...
0
by: frankenberry | last post by:
I have multi-page tiff files. I need to extract individual frames from the multi-page tiffs and save them as single-page tiffs. 95% of the time I receive multi-page tiffs containing 1 or more black...
6
by: cody | last post by:
What are multi file assemblies good for? What are the advantages of using multiple assemblies (A.DLL+B.DLL) vs. a single multi file assembly (A.DLL+A.NETMODULE)?
4
by: mimmo | last post by:
Hi! I should convert the accented letters of a string in the correspondent letters not accented. But when I compile with -Wall it give me: warning: multi-character character constant Do the...
5
by: Shane Story | last post by:
I can seem to get the dimensions of a frame in a multiframe tiff. After selecting activeframe, the Width/Height is still really much larger than the page's actual dimensions. When I split a...
5
by: bobwansink | last post by:
Hi, I'm relatively new to programming and I would like to create a C++ multi user program. It's for a project for school. This means I will have to write a paper about the theory too. Does anyone...
0
by: Sabri.Pllana | last post by:
We apologize if you receive multiple copies of this call for papers. *********************************************************************** 2008 International Workshop on Multi-Core Computing...
1
by: mknoll217 | last post by:
I am recieving this error from my code: The multi-part identifier "PAR.UniqueID" could not be bound. The multi-part identifier "Salary.UniqueID" could not be bound. The multi-part identifier...
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
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: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
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.