Python 列表 extend() 方法
定义和用法
extend() 方法将指定的列表元素(或任何可迭代的元素)添加到当前列表的末尾。
实例
例子 1
把 cars 中的元素添加到 fruits 列表:
fruits = ['apple', 'banana', 'cherry'] cars = ['Porsche', 'BMW', 'Volvo'] fruits.extend(cars)
例子 2
把元组添加到 fruits 列表:
fruits = ['apple', 'banana', 'cherry'] points = (1, 4, 5, 9) fruits.extend(points)
语法
list.extend(iterable)
参数值
| 参数 | 描述 |
| iterable | 必需。任何可迭代对象(列表、集合、元组等)。 |