Is there a way to append/extend a list with another list at a specific index in python? -
In other words, I want to complete something like the following:
a = [1, 2, 3, 7, 8] B = [4, 5, 6] # There is a list on index 3 to include List B in some magic lists so that one = [1, 2, 3, 4, 5, 6, 7, 8]
you type I tried to find a docs link that explicitly mentions this behavior, but fail (if you can find it, feel free to add one Please). So, I would just say that In short, this is just another awesome feature of Python. :) a
Only:
& gt; & Gt; & Gt; A = [1, 2, 3, 7, 8] & gt; & Gt; & Gt; B = [4, 5, 6]> gt; & Gt; & Gt; A [3: 3] = B & gt; & Gt; & Gt; A [1, 2, 3, 4, 5, 6, 7, 8] & gt; & Gt; & Gt;
[3: 3]
may seem strange, but it is necessary to enter the item b
in the list one < / Code> Otherwise, if we are assigned an index of
a
, list b
, then it will be inserted:
& Gt; & Gt; & Gt; A = [1, 2, 3, 7, 8] & gt; & Gt; & Gt; B = [4, 5, 6]> gt; & Gt; & Gt; A [3] = B & gt; & Gt; & Gt; A [1, 2, 3, [4, 5, 6], 8]> gt; & Gt; & Gt;
a [3: 3] = b
tells the dragon to move the list to the item b
and put it in the list section is represented by a
by [3: 3]
. In addition, the dragon will expand the list of a
needed to adjust this operation.