Bug or Feature?
Steven D. Majewski
sdm7g at Virginia.EDU
Fri Apr 23 13:00:16 EDT 1999
On Fri, 23 Apr 1999, Fuming Wang wrote:
>
> Hi,
>
> I found this little surprise with Python 1.5.1:
>
>
> >list = [[0]*2]*4
> >list
> [[0, 0], [0, 0], [0, 0], [0, 0]]
> >list[0][1] = 9
> >list
> [[0, 9], [0, 9], [0, 9], [0, 9]]
>
> Is this a bug or a feature that I don't know about?
>
It's a Frequently Discussed Feature.
[0]*2 concatenates two _copies_ of the list [0] to each other, and
the "*4" concatenates four _copies_ of that list together.
While we're on the subject of map (in another thread nearby), try:
list = [[0]*2]*4
list = map( lambda x: x[:], list )
list[0][1] = 9
( Looks like copy.deepcopy is too smart to do what I want here. )
---| Steven D. Majewski (804-982-0831) <sdm7g at Virginia.EDU> |---
---| Department of Molecular Physiology and Biological Physics |---
---| University of Virginia Health Sciences Center |---
---| P.O. Box 10011 Charlottesville, VA 22906-0011 |---
More information about the Python-list
mailing list