inserting into a list

Steven D'Aprano steve at
Tue Mar 7 23:19:58 CET 2006

On Tue, 07 Mar 2006 12:26:00 -0800, James Stroud wrote:

> John Salerno wrote:
>> Diez B. Roggisch wrote:
>>> Why don't you just _try_ that? It would have been way faster than to ask
>>> questions you can easily answer yourself.
>> I did try it, but I was still hoping for an explanation, which I've also 
>> gotten from you guys, some in nicer terms than others.
> People who answer questions on this list have forgotten how unintuitive 
> intuitive can be. In other words, they have found that the intuitive way 
> to do things in python is usually the right way, which may not be the 
> case in other languages. Thus, your instinct to see if your instincts 
> are correct rings as laziness here, when in fact you are just being 
> rigorous.

Not rigorous. Perhaps thorougher.

Had the OP worded the question more rigorously, we wouldn't be having this

"I wanted to see what happens if you try to insert a list into a list
using slicing, and discovered that this works:

L[2:2] = [ [1,2,3] ]

Now I don't understand the reasoning behind this. Can somebody explain the
rationale between needing to wrap objects in a list in order to insert
using slices?"

To which the answer would be, so it is consistent with other slice

L[2:10] = [1, 2, 3, 4]

Retrieving a slice returns a list. Assigning to a slice requires a list.
Making an exception for the special case of L[x:x] goes against
the philosophy of the Python language: Python generally doesn't accept
that special cases are special enough to break the rules.

Half the battle is asking the right question. The other half of the battle
is asking the right question in the right way.


More information about the Python-list mailing list