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)
自己动手试一试 »