How to insert a list into another list in python?

I have two lists:

A = [1,2,3] B = [4,5,6] 

Is there an elegant way to insert B in at an arbitrary position?

Hypothetical conclusion:

 [1,4,5,6,2,3] 

Obviously, I could iterate through B and embed them one at a time, but I decided there was a better way.

+6
source share
2 answers
 def insert(outer, inner, pos): outer[pos:pos] = inner 
+2
source
  A[1:1]=B 

A will be [1, 4, 5, 6, 2, 3]

+12
source

Source: https://habr.com/ru/post/894468/


All Articles