[Tutor] could someone explain why this happens to me.

Lie Ryan lie.1296 at gmail.com
Sun Mar 8 12:59:30 CET 2009


Sander Sweers wrote:
> 2009/3/7 Alan Gauld <alan.gauld at btinternet.com>:
>>> mycopy = original[:]
>> Returns a slice of the original list. In this case it so happens
>> the slice is the full list.
>>
>>> mycopy = list(original)
>> Use the list type constructor to make a list out of its argument.
>> It just so happens the argument in this case is a list.
> 
> Both not give the desired result with nested lists and this is why you have..

What is the desired result?

Sometimes I might want to copy a list, but keep the referenced items the 
same. In which case I'd use the slice copying or list constructor copying.

Any way, there is one unmentioned way to copy a list:

listcopy = [x for x in mylist]

though it might be slower and more verbose.



More information about the Tutor mailing list