Python - 在一個列表中插入另一個列表


列表還提供 extend() 方法,該方法從你作為引數傳入的列表中新增專案。extend() - Python 列表方法 extend() 將 seq 的內容追加到 list。你可以在此處 "https://tutorialspoint.tw/python/list_append.htm" 閱讀更多相關資訊。

現在讓我們親自動手

示例

 線上演示

#append
first_list = [1,2,3,4,5]
second_list = [6,7,8,9,10]
first_list.append(second_list)
# print result using append
print("The list pattern using append is : " + str(first_list))
#extend
third_list_ = [11,12,13,14,15]
fourth_list = [16,17,18,19,20]
third_list_.extend(fourth_list)
print("The list pattern using extend is : " + str(third_list_))

輸出

The list pattern using append is : [1, 2, 3, 4, 5, [6, 7, 8, 9, 10]]
The list pattern using extend is : [11, 12, 13, 14, 15, 16, 17, 18, 19, 20]

更新時間:2020-05-16

257 次瀏覽

開啟您的 職業生涯

參加課程以獲得認證

開始
廣告
© . All rights reserved.