which methods to use?

Florian Leitner fleitner at REMcnioOVE.es
Wed Mar 28 20:25:29 EDT 2007


* s99999999s2003 at yahoo.com wrote, On 3/29/07 2:09 AM:
>   hi
> which is the best methods to use for list copying. using list() , [:]
> or create list through list comprehension. what are the differences
> between these ?
> thanks
> 

depends on what you intend to do:

-create a new object:
use a list comprehension or list()

-make a shallow copy:
use copy.copy() or the slicing operator ([:])

-make a deep copy (incl. each list element):
use copy.deepcopy()

Try experimenting around, deleting and adding elements or changing them 
in the copy of your list and the original and see what happens if you 
are not sure what these things do. (to use copy, import it first)

flo



More information about the Python-list mailing list