[Tutor] list copy and remove question

Brian van den Broek bvande at po-box.mcgill.ca
Fri Sep 10 06:11:13 CEST 2004


Lloyd Kvam said unto the world upon 2004-09-09 20:47:
> On Thu, 2004-09-09 at 19:44, Brian van den Broek wrote:

<SNIP>

>>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.

Hi Lloyd,

Slap! (Sound of palm to forehead.)

Thanks,

Brian



More information about the Tutor mailing list