python中怎么合并两个列表

在Python中,可以使用两种方法来合并两个列表:

  1. 使用"+"操作符
list1 = [1, 2, 3]
list2 = [4, 5, 6]

merged_list = list1 + list2
print(merged_list)
  1. 使用extend()方法
list1 = [1, 2, 3]
list2 = [4, 5, 6]

list1.extend(list2)
print(list1)

两种方法都可以将两个列表合并成一个新的列表。

Both comments and pings are currently closed.

Comments are closed.

Powered by KingAbc | 粤ICP备16106647号-2 | Loading Time‌ 0.364