strange behaviour with remove

Panard panard at inzenet.org
Sat Jun 19 12:51:22 CEST 2004


Ok, I reply to myself, it's working when you add, line 3
l = [ i for i in l ]

I think the reason is that when you do a remove on d[ 'list' ] it will also
do a remove on l, because, I think, l in the dictionnary is only a
reference...


Panard wrote:

> Hi!
> 
> Can anyone explain this to me :
> $ cat test.py
> l = [ 1, 2, 3 ]
> d = { 'list' : l }
> 
> for x in l :
>         print "rm", x
>         d[ 'list' ].remove( x )
>         print "l =", l
> 
> print d
> 
> $ python test.py
> rm 1
> l = [2, 3]
> rm 3
> l = [2]
> {'list': [2]}
> 
> 
> Why 2 isn't removed ? and why l is changing during the loop ??
> Am I missing something ?
> 
> My python is 2.3.4
> 
> Thanks
> 

-- 
Panard



More information about the Python-list mailing list