Python 列表 extend() 方法

实例

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

  1. fruits = ['apple', 'banana', 'cherry']
  2.  
  3. cars = ['Porsche', 'BMW', 'Volvo']
  4.  
  5. fruits.extend(cars)

定义和用法

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

语法

  1. list.extend(iterable)

参数值

参数 描述
iterable 必需。任何可迭代对象(列表、集合、元组等)。

更多实例

实例

把元组添加到 fruits 列表:

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