Python 列表 extend() 方法
示例
將 cars
的元素新增到 fruits
列表中
fruits = ['apple', 'banana', 'cherry']
cars = ['Ford', 'BMW', 'Volvo']
fruits.extend(cars)
自己動手試一試 »
定義和用法
extend()
方法將指定的列表元素(或任何可迭代物件)新增到當前列表的末尾。
語法
list.extend(iterable)
引數值
引數 | 描述 |
---|---|
iterable | 必需。任何可迭代物件(列表、集合、元組等) |
更多示例
示例
將一個元組新增到 fruits
列表中
fruits = ['apple', 'banana', 'cherry']
points = (1, 4, 5, 9)
fruits.extend(points)
自己動手試一試 »