LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-05-2012, 07:16 PM   #1
Adwat
LQ Newbie
 
Registered: May 2012
Posts: 14

Rep: Reputation: Disabled
Changing one element of a 2-D list in python


I have the following code:

a=[[0]*2]*3

print a

a[1][1]=2 print a

the output is: [[0,0],[0,0],[0,0]] [[0,2],[0,2],[0,2]]

Why is it changing all the elements of the list?

Shouldn't the output be:

[[0,0],[0,2],[0,0]]

What needs to be done if I just want to change one of the element?

Thanks for the help!
 
Old 06-05-2012, 10:06 PM   #2
evo2
Senior Member
 
Registered: Jan 2009
Location: Japan
Distribution: Debian
Posts: 4,822

Rep: Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022Reputation: 1022
Hi,

this is because everything (except floats, ints etc) are references in python. Your 3 element array contains three references to the same two element array.

Try declaring your array like:
Code:
a = [ [0]*2 for i in range(3) ]
Evo2.
 
Old 06-06-2012, 06:36 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,366

Rep: Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910Reputation: 1910
Moved: This thread is more suitable in Programming and has been moved accordingly to help your thread/question get the exposure it deserves.
 
  


Reply

Tags
list, python


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Makefile index of list element shamgar03 Programming 7 04-29-2013 09:06 AM
Could not init font path element unix/:7100, removing from list! jeffreybluml Fedora 18 10-12-2011 08:06 AM
[EN][ES]python based graphic element for kubuntu togo Programming 1 12-11-2009 04:09 PM
type of list element in Perl ? thelonius Programming 2 06-20-2006 08:49 AM
Inserting element to a list of list in C suwandy_chandra Programming 2 03-09-2004 03:08 AM


All times are GMT -5. The time now is 06:34 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration