今天要跟大家介紹Python list物件的一個方法list.extend(iterable)
以下是官方文件的介紹
Extend the list by appending all the items from the iterable. Equivalent to a[len(a):] = iterable.
描述
使用可迭代(iterable)對象中的所有元素來擴展列表。相當於 a[len(a):] = iterable 。
語法
list.extend(seq)
參數
- seq--列表(list)
回傳值
該方法沒有回傳值,但會在已存在的列表中添加新的列表內容。
實例
aList = [123, '321', 'xyz', True, 2.4]
bList = ['hru', False, 456, 99.9]
#將aList末端延伸bList
aList.extend(bList)
#[123, '321', 'xyz', True, 2.4, 'hru', False, 456, 99.9]
print('extended list:', aList)