Python - 将列表插入另一个列表

pythonserver side programmingprogramming更新于 2026/1/4 13:00:17

列表还有 extend() 方法,该方法会将您作为参数传递的列表中的项目附加到列表中。extend() − Python 列表方法 extend() 将 seq 的内容附加到列表中。您可以在此处阅读更多相关信息"https://www.tutorialspoint.com/python/list_append.htm"

现在让我们亲手实践一下

示例

#append
first_list = [1,2,3,4,5]
second_list = [6,7,8,9,10]
first_list.append(second_list)
# 使用 append 打印结果
print("使用 append 的列表模式是:" + str(first_list))
#extend
third_list_ = [11,12,13,14,15]
fourth_list = [16,17,18,19,20]
third_list_.extend(fourth_list)
print("使用 extend 的列表模式是:" + str(third_list_))

输出

使用 append 的列表模式为:[1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
使用 extend 的列表模式为:[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

相关文章


有用资源