[Python-ideas] Operator for inserting an element into a list
mikhailwas at gmail.com
Tue Jun 12 19:42:47 EDT 2018
On Tue, Jun 12, 2018 at 10:26 PM, Terry Reedy <tjreedy at udel.edu> wrote:
> On 6/12/2018 10:54 AM, Mikhail V wrote:
>> Though additional brackets look really confusing for this purpose,
>> so I don't feel like using this seriously.
> Learning about lists means learning about slice assignment: replace a
> sublist with another sequence.
Yes I see, actually that is what I am saying - slice assignment has
If that would be a list method, I suppose it would work something like:
L = L.replace(begin, end, L2)
So that makes perfect sense.
But returning to append/insert.
So appending of course is extremely frequent operation,
inserting in the beginning of the list may be also not rare.
Concatenation in my opinion might be not so frequent.
Writing values from one array (or slice) to another is quite frequent
operation, that means slices of the same size.
As for _replacing_ slices, of different size, I personally never
used it. OTOH it smells like generalization.
Anyway, I prefer to look at it through 'syntax glass'.
Here is something ubiquitous:
L = L.append("string1")
L = L.append("string2")
L = L.append("string3")
L = L.append([1,2,3])
Could be written:
L ^= "string1"
L ^= "string2"
L ^= "string3"
L ^= [1,2,3]
These kind of things are basically in every second python script.
I'm ok with append() actually, but it just asks for a shortcut.
For the ^ character - yes it looks strange.
And writing it like this:
L += ["string1"]
L += ["string2"]
L += ["string3"]
L += [[1,2,3]]
Sorry, no, that would be too much for me, also this needs some real
mental training so as not to misinterpret these.
Append() is just fine, though it's a pity there is no shortcut operator.
More information about the Python-ideas