From: sdm7g at Virginia.EDU (Steven D. Majewski)
Date: Fri, 23 Apr 1999 13:00:16 -0400 (EDT)
Subject: Bug or Feature?
In-Reply-To: <37208E69.4B022E0C@mediaone.net>
References: <37208E69.4B022E0C@mediaone.net>
Message-ID: <Pine.A32.3.90.990423125354.23052C-100000@elvis.med.Virginia.EDU>
X-UID: 732                                                  

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  |---





