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

Hrvoje Niksic hniksic at xemacs.org
Mon Jul 12 07:20:45 EDT 2010


dhruvbird <dhruvbird at gmail.com> writes:

> No, I meant x.append(4)
> Except that I want to accomplish it using slices.
>
> (I can do it as x[lex(x):] = [item_to_append] but is there any other
> way?)

It seems that you've found a way to do so, so why do you need another
way?  Are you after elegance?  Efficiency?  Brevity?

Here are some other ways to express the same, and all use slices in some
way:

x[slice(len(x), None)] = [item_to_append]
x.__setitem__(slice(len(x), None), [item_to_append])
x.__setslice__(len(x), len(x), [item_to_append])

...but I have no idea why any of them would make any more sense than
x[len(x):] = [item_to_append].



More information about the Python-list mailing list