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

Greg Ewing greg.ewing at canterbury.ac.nz
Wed Jun 13 19:58:21 EDT 2018

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.

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.


   L.insert(i, item)

doesn't have any of those problems, and again is mostly

Python is not Perl. Not every operation that you use more than
once in a blue moon needs to have its own operator.


More information about the Python-ideas mailing list