[Tutor] list copy and remove question
Lloyd Kvam
pythonTutor at venix.com
Fri Sep 10 02:47:41 CEST 2004
On Thu, 2004-09-09 at 19:44, Brian van den Broek wrote:
> 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.
That would happen if remove returned self. However, it returns None.
So s is set to None. 2 is removed from r[:], but that object is no
longer accessible since it is not bound to any name.
> 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
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Lloyd Kvam
Venix Corp
More information about the Tutor
mailing list