[Tutor] how to *really* copy a list

kevin parks kp8 at mac.com
Sun Apr 30 00:55:08 CEST 2006


John,

Thanks. Your message was very helpful. I will tattoo it to my forehead.
hehe... i notice that the "learning python" book also explains so of 
this
and i shall study that as well....

cheers,

kevin

On Apr 27, 2006, at 10:14 PM, tutor-request at python.org wrote:
>
> On 28/04/06, kevin parks <kp8 at mac.com> wrote:
>> In most case you are fine operating on the list in place and altering 
>> the
>> existing list. In some cases you want your code to stop molesting 
>> your poor
>> mutables and really honestly sincerly copy the dang thing. In this 
>> case i am
>> making a function that does odd smmetry mirroring. But i want my 
>> orginal list
>> to remain intact....
>>
>> def mirror(seq):
>>         """odd symmetry mirroring  [1, 2, 3, 4] --> [1, 2, 3, 4, 3, 
>> 2, 1]"""
>>         foo=seq[:-1]                            # copy list, 
>> excluding last element for odd symetry
>>         foo.reverse()                           # flip it
>>         seq.extend(foo)
>>         return seq
>
> Hi Kevin,
>
> Your problem is this line:
>     seq.extend(foo)



More information about the Tutor mailing list