Why doesn't python's list append() method return the list itself?
Steven D'Aprano
steve at REMOVE-THIS-cybersource.com.au
Sun Jul 11 20:24:09 EDT 2010
On Sun, 11 Jul 2010 08:59:06 -0700, 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, but that rules out
> constructs like:
> ([1,2,3,4].reverse()+[[]]).reverse()
Yes. So what? Where's the problem?
List methods work in place. If you're annoyed now, that's *nothing* to
the annoyance you'll feel if they returned the list and you did this:
alist = [1,2,3]
blist = alist.append(4) # Make a new list with 4 appended.
assert alist == [1,2,3]
> 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].
Or:
L = [[]]
L.extend([1,2,3,4])
Or:
L = [1,2,3,4]
L.insert(0, [])
Not everything needs to be a one-liner.
--
Steven
More information about the Python-list
mailing list