942,504 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Marked Solved
  • Views: 29641
  • Python RSS
Oct 22nd, 2006
0

Making a List of Lists

Expand Post »
I was trying to create a 3x3 list of lists, and came up with this surprising behaviour:
Python Syntax (Toggle Plain Text)
  1. # creating a 2D list with an overloaded * operator
  2. mlist3 = [[0]*3]*3
  3. print mlist3 # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  4. mlist3[0][0] = 1
  5. print mlist3 # [[1, 0, 0], [1, 0, 0], [1, 0, 0]] oops!
  6. # populate it properly, now it works!
  7. mlist3[0] = [7, 12, 23]
  8. mlist3[1] = [22, 31, 9]
  9. mlist3[2] = [4, 17, 31]
  10. print mlist3 # [[7, 12, 23], [22, 31, 9], [4, 17, 31]]
  11. mlist3[0][0] = 1
  12. print mlist3 # [[1, 12, 23], [22, 31, 9], [4, 17, 31]]
Any explanation?
Last edited by sneekula; Oct 22nd, 2006 at 10:33 pm.
Similar Threads
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 23rd, 2006
0

Re: Making a List of Lists

Yeah, I posted about this, too. Here's the deal:

Because lists are mutable, multiple references to the same list can lead to side effects.

This line:

Python Syntax (Toggle Plain Text)
  1. mlist = [[0]*3]*3

creates a list with three references to the single list [0,0,0].

So when you change that list, all of the references get "automagically" updated.

I haven't decided yet whether this is a "feature" of Python or a "bug." :p

Initializing the array "manually" works, though:

Python Syntax (Toggle Plain Text)
  1. for i in range(3):
  2. a.append([0,0,0])
  3. >>> a
  4. [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
  5. >>> a[0][0] = 1
  6. >>> a
  7. [[1, 0, 0], [0, 0, 0], [0, 0, 0]]
  8. >>>

Jeff
Reputation Points: 92
Solved Threads: 156
Practically a Master Poster
jrcagle is offline Offline
608 posts
since Jul 2006
Oct 23rd, 2006
0

Re: Making a List of Lists

Thanks Jeff, so multiplying the sublist [0, 0, 0] by three makes three of exactly the same sublist having the same reference. Appending the list properly won't do that! Wanted to be tricky and paid the price!
Reputation Points: 961
Solved Threads: 211
Nearly a Posting Maven
sneekula is offline Offline
2,413 posts
since Oct 2006
Oct 23rd, 2006
0

Re: Making a List of Lists

The id() test sheds some more light on this ...
[php]mlist3 = [[0]*3]*3
print mlist3 # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# give it the id() test ...
# all three sublists objects show the same address, 10317016 on my machine
# so they are alias copies, not true copies
print mlist3[0], id(mlist3[0]) # [0, 0, 0] 10317016
print mlist3[1], id(mlist3[1]) # [0, 0, 0] 10317016
print mlist3[2], id(mlist3[2]) # [0, 0, 0] 10317016

mlist4 = []
for k in range(3):
mlist4.append([0, 0, 0])

print mlist4 # [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# the id() test shows them to be different objects
print mlist4[0], id(mlist4[0]) # [0, 0, 0] 10316936
print mlist4[1], id(mlist4[1]) # [0, 0, 0] 10316976
print mlist4[2], id(mlist4[2]) # [0, 0, 0] 10317096
[/php]
Moderator
Reputation Points: 1333
Solved Threads: 1403
DaniWeb's Hypocrite
vegaseat is offline Offline
5,789 posts
since Oct 2004
Nov 30th, 2009
0

Manually? Yikes!

I have this same problem, but with a matrix that's 50x1448. i have to manually enter a list with 1448 zeros? that's a nasty surprise.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
mike_bow is offline Offline
1 posts
since Nov 2009
Nov 30th, 2009
0
Re: Making a List of Lists
If by manually you mean with 2 for loops or a list comprehension.

Python Syntax (Toggle Plain Text)
  1. listX = [[0 for i in range(1448)] for j in range(50)]
Reputation Points: 12
Solved Threads: 15
Junior Poster in Training
Mathhax0r is offline Offline
64 posts
since Aug 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: Flip
Next Thread in Python Forum Timeline: Subclassing dict





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC