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

Erik python at lucidity.plus.com
Mon May 22 04:52:09 EDT 2017


On 21/05/17 15:43, Paul Laos wrote:
>  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.

I don't think list.insert() with an index of -1 does what you think it does:

$ python3
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
 >>> l = [0, 1, 2]
 >>> l
[0, 1, 2]
 >>> l.insert(-1, 99)
 >>> l
[0, 1, 99, 2]
 >>>

Because the indices can be thought of as referencing the spaces 
_between_ the objects, having a push() in which -1 is referencing a 
different 'space' than a -1 given to insert() or a slice operation 
refers to would, I suspect, be a source of confusion (and off-by-one bugs).

E.


More information about the Python-ideas mailing list