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

Alan Gauld alan.gauld at btinternet.com
Sat Mar 7 21:02:05 CET 2009


"Emad Nawfal (عماد نوفل)" <emadnawfal at gmail.com> wrote

> As a linguist, I would love to know what the difference between 
> these
> things are:

The differences are the same whether you are a linguist or not :-)
(and yes I know my reading is incorrect grammaticallly but I couldn't 
resist it!)

> mycopy = original[:]
Returns a slice of the original list. In this case it so happens
the slice is the full list.

> mycopy = copy.deepcopy(original)

calls the deepcopy function which traverses the original list
and all nested structures explicitly copying the elements.

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

The end result is the same but the mechanisms (ie the code called)
are quite different. It would be interesting to compare speeds,
but I'm too lazy! I suspect deepcopy would be slowest but I'm
not sure about the other two.

Alan G.





More information about the Tutor mailing list