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

Mikhail V mikhailwas at gmail.com
Wed Jun 13 21:45:04 EDT 2018


On Thu, Jun 14, 2018 at 2:58 AM, Greg Ewing <greg.ewing at canterbury.ac.nz> wrote:
> Mikhail V wrote:
>>
>> L ^= item
>> is
>> L.append(item)
>> or
>> L += [item]
>
>
> Okay, that achieves an in-place append, but it's not exactly
> obvious to the unenlightened what it does, whereas append()
> is pretty self-explanatory.
>

Sure, I use append() only and obviously would recommend to do so. I am not
sure though everything's so simple here.
Yet by writing examples here I've made typos several times, like L =
L.append() -
so strong is the influence of functional programming.

I don't like cryptic operators, although such cases make me think in-place
operations need some specialty in their syntax and augmented
assignment could provide something positive in this regard.

Another point is that people do like augmented operators much and for the
append - there are so many advises like: hey, use L += [item] !
But Imo this just makes things worse from both practical and semantics POV
(still I find it hard to explain why exactly, that's just some gut feeling).

Not to argue, since I personally wouldn't benefit much from the idea,
but it just seems important, more than just 'cosmetics'.


> Also, using the slice version to do an insert
>
>   L[i:i] ^= item
>
> is not as efficient as it looks like it should be, because it
> creates an empty list, appends the item to it and then splices
> that back into the list. And you have to write the index twice.

Efficiency is important ... where it is important.


More information about the Python-ideas mailing list