Python 列表 extend() 方法

實例

把 cars 中的元素添加到 fruits 列表:

fruits = ['apple', 'banana', 'cherry']
cars = ['Porsche', 'BMW', 'Volvo']
fruits.extend(cars)

運行實例

定義和用法

extend() 方法將指定的列表元素(或任何可迭代的元素)添加到當前列表的末尾。

語法

list.extend(iterable)

參數值

參數 描述
iterable 必需。任何可迭代對象(列表、集合、元組等)。

更多實例

實例

把元組添加到 fruits 列表:

fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)

運行實例