[Python-ideas] Suggestion: push() method for lists

Nick Coghlan ncoghlan at gmail.com
Mon May 22 01:47:42 EDT 2017


On 22 May 2017 at 00:43, Paul Laos <paul_laos at outlook.com> wrote:
> So while it has been discussed before, it's worth bringing up again, since
> this was before the release of Python 2.0.

It was also before the addition of collections.deque. which uses
appendleft() and extendleft(), rather than pushleft().

> Pros:
>
> - Would simplify the language by having a symmetric relation to pop().
> - Would make it easy to use lists as stacks.

I think the key argument here would be that *for folks that already
know the push/pop terminology for stack data structures*, "push" is a
potentially more intuitive term than the insert/append/extend trio.

However, for most people, "append an item to a list", "insert an item
into a list" and "extend a list" are common English phrases, while
"push an item onto a list" would get you funny looks, and even "push
an item onto a stack" would be unusual in a spoken conversation (the
non-jargon phrase in that case is "add an item to the stack", but
"add" would be ambiguous between the append() and extend() meanings)

As a result, the specific-to-computer-science jargon loses out.

The situation for `pop()` is different, as `remove()` is already taken
for "remove an item from the list by value", so a different word is
needed for "remove an item from the list by index".

> - If append()/insert() are being removed and replaced, the complexity of
> lists is slightly reduced.

There's zero chance of the existing APIs going away - they're not
broken, and they match common English phrasing. The fact they don't
match common computer science jargon isn't ideal, but it's
relevatively straightforward to define a stack data structure if
someone really wants to do so.

> Cons:
>
> - Would blur the line between lists and stacks.
>
> - The order of the parameters in push(obj, index = -1) would be the opposite
> of the parameters in insert(index, obj), because defaulted parameters come
> last.

While I don't think it makes sense to add the method in the first
place, if we did, it either wouldn't accept an index parameter, or
else the index parameter would be a keyword-only argument.

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-ideas mailing list