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

Ryan Gonzalez rymg19 at gmail.com
Tue Jun 12 22:39:41 EDT 2018


^ is also used in regexes for matching the *beginning* of a string... 
Realistically, I don't think this proposal would be added, but if it were, 
^ would be a horrible choice.

That being said, I do understand the feeling of half your code being calls 
to .append or .extend. You could always do:

L += 'string1'

Only thing extra is the comma at the end; it converts the single value to a 
tuple, which is then added to the list.

As for the use of +, I think that's mostly opinion. Many other languages 
use + for concatenation, so Python's hardly on its own here.


On June 12, 2018 6:43:18 PM Mikhail V <mikhailwas at gmail.com> wrote:

> 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
> _replace_ semantics.
> 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.
>
>
>
> M
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/




More information about the Python-ideas mailing list