strange side effect with lists!?

Michael Hoffman m.h.3.9.1.without.dots.at.cam.ac.uk at example.com
Wed Oct 13 04:52:03 EDT 2004


Wolfgang.Stoecher at profactor.at wrote:
> 
>>>>l1 = [1] # define list
>>>>l2 = l1  # copy of l1 ?

Nope, l2 is l1:

 >>> l2 is l1
True

The same happens for any mutable object.

If you want to just do a surface-level copy of l1, you can use l1[:]. 
But if any of the items in l1 are mutable objects, you probably want to 
use copy.deepcopy().
-- 
Michael Hoffman



More information about the Python-list mailing list