How do I insert a list into another list?

2020-02-06 02:54发布

I have two lists:

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

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

Hypothetical output:

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

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

标签: python list
2条回答
▲ chillily
2楼-- · 2020-02-06 02:54
def insert(outer, inner, pos):
  outer[pos:pos] = inner
查看更多
贪生不怕死
3楼-- · 2020-02-06 03:15
A[1:1] = B

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

查看更多
登录 后发表回答