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

Clint Hepner clint.hepner at gmail.com
Tue Jun 12 12:42:24 EDT 2018


> On 2018 Jun 12 , at 10:54 a, Mikhail V <mikhailwas at gmail.com> wrote:
> 
> 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"]

-1. There's not much about this that is logical, no matter how much
you want an insertion operator. Even if L[0] /= "bb" worked, then logically
so should L[0] = L[0] / "bb". However, there is no sense in which L[0] / "bb"
by itself has any meaning, and what would L[1] = L[0] / "bb" mean?

And finally, L[0] /= x (and really, every other augmented operator) *already has* a meaning:

>>> L = [10]
>>> L[0] /= 2
>>> L
[5]


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

It's no more confusing than co-opting an unrelated operator to do the same thing.

--
Clint


More information about the Python-ideas mailing list