obj2 = ojb1 = range(len(somelist))

Sean 'Shaleh' Perry shalehperry at attbi.com
Thu Jan 31 18:46:38 EST 2002


On 31-Jan-2002 David Bear wrote:
> I wanted to create 2 distinct lists that were derived from range
> function.  rather that doing
> 
> obj1 = range(len(somelist))
> obj2 = range(len(somelist))
> 
> I wanted to do
> 
> obj2 = ojb1 = range(len(somelist))
> 
> Yet, both names point to the same object -- they're not two objects.
> 
> Is there some other syntax that I could use to avoid having to call
> range(len(list)) twice and still create two objects?
> 

to summarize the other posts, when you copy a list you get a shallow copy (they
all point to the same place).  The only way around this is to force a full copy
of the list which ends up being about as good as just calling range again.

list[:] works becuase it creates a new list

import copy
copy.copy() implements a deep copy, which is basically the same as list[:].




More information about the Python-list mailing list