LinuxQuestions.org
Help answer threads with 0 replies.
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
 
Search this Thread
Old 06-05-2012, 08: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, 11:06 PM   #2
evo2
Guru
 
Registered: Jan 2009
Location: Japan
Distribution: Mostly Debian and Scientific Linux
Posts: 5,588

Rep: Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227Reputation: 1227
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, 07:36 PM   #3
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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


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


All times are GMT -5. The time now is 01:24 AM.

Main Menu
Advertisement
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