initialized list: strange behavior

J

Jason Scheirer

Hi Python experts! Please explain this behavior:
nn=3*[[]]
nn [[], [], []]
mm=[[],[],[]]
mm

[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!
mm[1].append(17)
mm [[], [17], []]
nn[1].append(17)
nn

[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!

You're creating three references to the same list with the
multiplication operator. You can easily get the same behavior because
of similar mechanics in a more common scenario:

In [1]: a = []

In [2]: b = a

In [3]: a, b
Out[3]: ([], [])

In [4]: a.append(1000000)

In [5]: a, b
Out[5]: ([1000000], [1000000])

Python is pass-by-reference, not pass-by-value.
 
G

Gary Herron

Hi Python experts! Please explain this behavior:

[] make an empty list.
[ [],[],[] ] makes a list of three different empty lists.
3*[[]] makes a list of three references to the same list.

Realy, that should explain it all, but perhaps there are enough empty
lists here to obscure things. Try this:

Here b is a list that contains three references to a. Modify a, and
all three references to a show the modification:
a = [1,2,3]
b = [a,a,a]
a.append(4)
b
[[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]

Gary Herron


[[], [], []]
[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!

[[], [17], []]
[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!
 
A

Arnaud Delobelle

Jason Scheirer said:
Hi Python experts! Please explain this behavior:
nn=3*[[]]
nn [[], [], []]
mm=[[],[],[]]
mm

[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!
mm[1].append(17)
mm [[], [17], []]
nn[1].append(17)
nn

[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!

You're creating three references to the same list with the
multiplication operator.

There's no need to introduce references: you're creating a list with the
same object at each position.

[...]
Python is pass-by-reference, not pass-by-value.

It's certainly not pass-by-reference, nor is it pass-by-value IMHO.
 
S

Steve Holden

Arnaud said:
Jason Scheirer said:
Hi Python experts! Please explain this behavior:

nn=3*[[]]
nn
[[], [], []]
mm=[[],[],[]]
mm
[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!

mm[1].append(17)
mm
[[], [17], []]
nn[1].append(17)
nn
[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!
You're creating three references to the same list with the
multiplication operator.

There's no need to introduce references: you're creating a list with the
same object at each position.

[...]
Python is pass-by-reference, not pass-by-value.

It's certainly not pass-by-reference, nor is it pass-by-value IMHO.
Since no lists are being passed as arguments in these examples it's not
pass-by-anything. Jump off that horse right now!

regards
Steve
 
S

Steve Holden

Arnaud said:
Jason Scheirer said:
Hi Python experts! Please explain this behavior:

nn=3*[[]]
nn
[[], [], []]
mm=[[],[],[]]
mm
[[], [], []]

Up till now, 'mm' and 'nn' look the same, right? Nope!

mm[1].append(17)
mm
[[], [17], []]
nn[1].append(17)
nn
[[17], [17], [17]]

???

Python 2.5 Win XP

Thanks!
You're creating three references to the same list with the
multiplication operator.

There's no need to introduce references: you're creating a list with the
same object at each position.

[...]
Python is pass-by-reference, not pass-by-value.

It's certainly not pass-by-reference, nor is it pass-by-value IMHO.
Since no lists are being passed as arguments in these examples it's not
pass-by-anything. Jump off that horse right now!

regards
Steve
 
A

alexander.genkin

The issue is exhausted in Python Library Reference, Chapter 3.6, so I
should apologize for initial posting. All comments were helpful,
though Arnaud and Steve are right that pass-by-anything is off the
point.

Thanks All!
 
A

Arnaud Delobelle

Steve Holden said:
Since no lists are being passed as arguments in these examples it's not
pass-by-anything. Jump off that horse right now!

You're right that Python's calling semantics have nothing to do with the
question asked. I just thought best not to leave the OP under any
misapprehension.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top