[Tutor] list copy and remove question

Brian van den Broek bvande at po-box.mcgill.ca
Fri Sep 10 01:44:59 CEST 2004


Hi all,

I'm back to Python after long enough of an interruption to make me feel a 
bit like a fresh-faced newbie again.

I've read around in the docs and done a bit of google, but haven't managed 
to understand:

Python 2.3.4 (#53, May 25 2004, 21:17:02)
 >>> r = [1, 2, 3]
 >>> s = r[:].remove(2)
 >>> print s
None
 >>> t = r[:]
 >>> t.remove(2)
 >>> print t
[1, 3]
 >>>

Both times I expected the second procedure's result. I get that remove 
modifies in place, but it seems to me like the condensed version should 
work. My thinking is that it first creates a copy, then modifies the copy 
in place, then assigns the modified copy to s. But it would seem that 
that's not what happens.

The 2 questions then are: why doesn't the first way work?, and, in the 
first way, from what (if anything) is 2 being removed?

Pointers to the doc's explanation that I must have overlooked are welcome.

Thanks and best to all,

Brian vdB



More information about the Tutor mailing list