Problem with assigning variables of type List

Sean 'Shaleh' Perry shalehperry at attbi.com
Mon Aug 19 03:25:15 EDT 2002


On 19-Aug-2002 Abhishek Roy wrote:
> Hello,
>       It appears that, 
> a = b = []
> is not the same as
> a=[]
> b=[]
> 
> because in the previous case a and b will refer to the same list eg,
>>>> b.append(23)
>>>> a
> [23]
> I am completely baffled by this. Can someone please explain?
> 

if you do:

b = range(1,1000)
a = b

you do not copy the 1,000 element list you just get another reference to it. 
If you really want to copy all of the elements (deep v. shallow copy) you need
to explicitly ask for it with:

import copy
a = copy.deepcopy(b)




More information about the Python-list mailing list