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

Mikhail V mikhailwas at gmail.com
Fri Jun 15 11:48:58 EDT 2018


On Fri, Jun 15, 2018 at 5:51 AM, Michael Selik <mike at selik.org> wrote:

> If you would like to prove the need for this operator, one piece of evidence
> you can provide is a count of the number of times someone writes
> "list.append" for an iterable vs "+=" and encloses a str or other type in a
> throw-away list to effectively append.

That's strange idea - there is no doubt that one would use
list.append() and most probably
it is the case statistically.
So the question would be "what is wrong with list.append()?"
And as said many times, there is nothing wrong, but a lot of people
seem to want an in-place
operator for this purpose. And I can understand this, because:

1. append() is _ubiquitous_
2. in-place assignment form makes some emphasis on mutating, in
contrast to method call.

That's it. So instead of a method call one gets a clean element on the
right-hand
and (hopefully) emphasis on the in-place nature of operation.

A quick google search shows some tendency:

https://stackoverflow.com/a/2022044/4157407
https://stackoverflow.com/a/28119966/4157407

So you shoudn't explain it to _me_ - I don't see other significant
convincing points,
and unless this gets support here - I am not interested in continuing.

>
> I see no benefit to this, because += already is an elegant way to extend a
> list, which is more flexible than append. Yes, if the right-hand is an
> iterable and should be appended as a single element, you'll need to enclose
> it in a single-element container. This is true for strings, lists, sets,
> whatever. It's natural and is not a "trick".
>

encouraging to mimic append() via += operator is bad practice.
In the above links to SO, people try to tell the same, but that is not something
that can be easily explained on paper. Now add here the constant wish for
using in-place operator - and there will be on-going confusion.
Regardless of how natural or general it is, it's something
that should be avoided.

Only in this discussion thread I had 3 different advices to use:

L += ["aa"]
L += ("aa",)
L += "aa",

But you should not give it to _me_  - i use the correct form only:

L.append("aa")


More information about the Python-ideas mailing list