[Python-ideas] Alternative spelling for list.append()

Mikhail V mikhailwas at gmail.com
Mon Jun 18 15:51:59 EDT 2018

On Mon, Jun 18, 2018 at 4:12 AM, Chris Angelico <rosuav at gmail.com> wrote:
> On Mon, Jun 18, 2018 at 10:50 AM, Steven D'Aprano <steve at pearwood.info> wrote:

>>>> items[-0:] # failed parallel
> ['spam', 'ham', 'foo', 'bar', 'quux']
> So you use -1 in slices to parallel 1 (unlike using ~1 as with
> indexing), and everything works *except zero*. Which means that the
> slice-assignment form of insert is easy to write, but the
> slice-assignment form of append isn't.
> Mikhail, if it were possible to append using slice assignment, would
> that meet the case?

How? like this:

items[-0:] = [item]

One of main motivation is actually to have just 'item' on the right side.
So your idea is to have special syntax for 'last item' so as to avoid
something like:

c = len(L)
L[c:c] = [...]

But IIUC you're still about _iterable_ on the right-hand part?

> However, this would be far from the first time
> that a much-hated-on proposal leads to one that has actual support,
> perhaps by restricting it, or maybe by generalizing it, or something.

I think it is currently quite restricted.
So imo it's rather generalizing that could make change.

Namely the idea for 'L[] = item'  seems plausible for append() method
only, and for the types that have this method.
Python array type has similar method set as lists.
Numpy arrays have also append() and insert() methods,
but they do not change arrays in-place.

So just for keeping it more or less focused -
assume we're considering the 'L[] = item' approach, say for mutable types.

So one syntax generalization possible is towards insert() method.
Although IMO it could be made into nice syntax only by introducing
some symbol into the index notation. For example:

L[] = item                 append(item)
L[^] = item                ?  appendleft(item)
L[^0] = item               insert (0, item)
L[^i] = item               insert(i, x)

Note, I am not responsible for the adequacy of the technical
part - since it may be not plausible technically.

So just speculating here: if that was added, then could it be linked to
corresponding methods?

For example for collections.deque type:

deq[] = item               deq.append(item)
deq[^] = item              deq.appendleft(item)
deq[^i] = item             deq.insert(i, item)

So syntactically - I would say it makes more or less complete
picture. Because insert() and append() come hand in hand semantically.

And with operator-only variant or without special symbol - it seems it is
not possible, (at least not with bareable index notation).

More information about the Python-ideas mailing list