Why doesn't python's list append() method return the list itself?
MRAB
python at mrabarnett.plus.com
Sun Jul 11 13:38:53 EDT 2010
Thomas Jollans wrote:
> On 07/11/2010 05:59 PM, dhruvbird wrote:
>> Why doesn't python's list append() method return the list itself? For
>> that matter, even the reverse() and sort() methods?
>> I found this link (http://code.google.com/edu/languages/google-python-
>> class/lists.html) which suggests that this is done to make sure that
>> the programmer understands that the list is being modified in place,
>
> Yes!
>
>> but that rules out constructs like:
>> ([1,2,3,4].reverse()+[[]]).reverse()
>
> No!
>
> you can either approach this by imperatively modifying a list in-place:
>
> L = [1,2,3,4]
> L.reverse()
> L.append([])
> L.reverse()
>
[snip]
If you want to prepend an empty list in-place, use the .insert method:
L = [1,2,3,4]
L.insert(0, [])
More information about the Python-list
mailing list