[Python-ideas] Operator for inserting an element into a list

Mikhail V mikhailwas at gmail.com
Tue Jun 12 10:54:43 EDT 2018

I think it would be logical to have the insert operator for lists.
Similar to list extend operator += , it could use one of augmented
assignment operators, e,g, /=.

    L = ["aa"]

    L[0] /= "bb"

    ->  ["bb", "aa"]

    L[0] /= [1,2]

    ->  [[1,2], "aa"]


Without index it would work like append():

    L /= "bb"

    #->  ["aa", "bb"]

As for possible spellings I like this one as well:

    L[i] ^= e

The proposed solution is meant to have insert() method semantics,
plus it would cover append() method nicely.

Insert and append are very frequent operations, so I wonder
if there was already related suggestion?   Is there some technical
problem with implementing this?

Note that there is a trick to 'insert' an element with slicing syntax, e.g.:

    L[0:0] = [[1,2]]

    -> [[1,2], "aa"]

    L[0:0] = ["bb"]

    -> ["bb", "aa"]

The trick is to put brackets around the element and so it works as insert().
Though additional brackets look really confusing for this purpose, so I don't
feel like using this seriously.


More information about the Python-ideas mailing list