trouble with lists
vincent wehren
vincent at visualtrans.de
Tue May 3 13:52:52 EDT 2005
<anandr86 at gmail.com> schrieb im Newsbeitrag
news:1115142163.472256.205930 at o13g2000cwo.googlegroups.com...
| I'm new to python. I tried doing this
|
| >>> x = [[]] * 3
| >>> print x
| [ [] [] [] ]
| >>> x[0].append( 2 )
| [ [2] [2] [2] ]
|
| I confused with the last line output. I actually expected something
| like
|
| [ [2] [] [] ]
|
| Can anyone give me an explanation. help!!
The library reference can!
See http://docs.python.org/lib/typesseq.html
Look at the section "notes (2)":
"""Values of n less than 0 are treated as 0 (which yields an empty sequence
of the same type as s). Note also that the copies are shallow; nested
structures are not copied. This often haunts new Python programmers;
consider:
>>> lists = [[]] * 3
>>> lists
[[], [], []]
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]"""etc.
--
Vincent Wehren
More information about the Python-list
mailing list