By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,402 Members | 1,047 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,402 IT Pros & Developers. It's quick & easy.

array of arrays question

P: n/a
Meo
Hi,

Here is what I'm doing in python 2.4.1 :
aOfa=[[[]]*3]*4
aOfa [[[], [], []], [[], [], []], [[], [], []], [[], [], []]] aOfa[3][2].append(1)
aOfa

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

Ok, so there is something I didn't understand about python's arrays.

The result I was expecting was :

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

Somebody understand what's going on here?

Thank

Aug 30 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Meo" <er*********@isrec.ch> writes:
Somebody understand what's going on here?


Yes, []*3 gives you three references to the same empty list, not three
separate empty lists. You need something like

[[] for i in xrange(3)]

to get separate lists.

Another example:

a = [1,2,3]
b = a
a[0] = 5
print b # prints [5,2,3]

Do you understand now?
Aug 30 '05 #2

P: n/a
Meo
yes,

thank you for your help

Aug 30 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.