Why doesn't python's list append() method return the list itself?

Antoine Pitrou solipsis at pitrou.net
Sun Jul 11 13:21:25 EDT 2010


On Sun, 11 Jul 2010 08:59:06 -0700 (PDT)
dhruvbird <dhruvbird at gmail.com> 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,
> but that rules out constructs like:
> ([1,2,3,4].reverse()+[[]]).reverse()
> I want to prepend an empty list to [1,2,3,4]. This is just a toy
> example, since I can always do that with [[]]+[1,2,3,4].

>>> x = [1,2,3,4]
>>> y = [5,6]
>>> x[:0] = y
>>> x
[5, 6, 1, 2, 3, 4]






More information about the Python-list mailing list