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

Paul Laos paul_laos at outlook.com
Sun May 21 10:43:12 EDT 2017

It would be nice to have the opposite of the pop() method: a push() method. While insert() and append() already exist, neither is the opposite of pop(). pop() has a default index parameter -1, but neither insert() nor append() has a default index parameter. push(obj) would be equivalent to insert(index = -1, object), having -1 as the default index parameter. In fact, push() could replace both append() and insert() by unifying them.

By default push(obj) would insert an object to the end of the list, while push(obj, index) would insert the object at index, just like pop() removes (and returns) the object at the end of the list, and pop(index) removes (and returns) the object at the index.

I found little discussion on this, just this SO thread http://stackoverflow.com/questions/1566266/why-is-pythons-append-not-push which lead to a discussion from 20 years ago (1997): <https://groups.google.com/forum/#!topic/comp.lang.python/SKJq3S2ZYm> https://groups.google.com/forum/#!topic/comp.lang.python/SKJq3S2ZYmg <https://groups.google.com/forum/#!topic/comp.lang.python/SKJq3S2ZYmg>

Some key arguments from the thread:

>it would be an
>easy and obvious improvement to make popend an explicit builtin, and
>that this would make Python even more attractive to newcomers who want
>to use what they already know and be immediately productive.
- Terry Reedy

>append() is a special case of insert().  The inverse of insert() is
>the del function.  The specific inverse of append() is del list[-1].
- Michael W. Ryan

>but I'm not a big fan of multiple names for the same operation --
>sooner or later you're going to read code that uses the other one, so
>you need to learn both, which is more cognitive load.
- Guido van Rossum

So while it has been discussed before, it's worth bringing up again, since this was before the release of Python 2.0.


- Would simplify the language by having a symmetric relation to pop().

- Would make it easy to use lists as stacks.

- Saves at least two characters

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


- 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.

- If append()/insert() are being removed and replaced, backwards compatability breaks.

- If append()/insert() are kept instead of being replaced, the complexity of lists is slightly increased.

While it isn't a necessity, I believe the benefit of push() method outweighs its cons.

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-ideas/attachments/20170521/898c3ef6/attachment.html>

More information about the Python-ideas mailing list