extend python_带有示例的Python列表extend()方法
extend python
列出extend()方法 (List extend() Method)
extend() method is used to extend a list, it extends the list by inserting the list of the elements at the end of the current list. The method is called with this list (current list, in which we have to add the elements) and another list (or any iterable) is supplied as an argument.
extend()方法用于扩展列表,它通过在当前列表的末尾插入元素列表来扩展列表。 用该列表(当前列表,我们必须在其中添加元素)调用该方法,并提供另一个列表(或任何可迭代)作为参数。
Syntax:
句法:
list_name.index(iterable)
Parameter(s):
参数:
iterable – It represents a list of the elements or any iterable.
可迭代 –表示元素列表或任何可迭代。
Return value:
返回值:
The return type of this method is
此方法的返回类型为
Example 1:
范例1:
# Python List extend() Method with Example
# declaring the lists
cars = ["Porsche", "Audi", "Lexus", "Audi"]
more = ["Porsche", "BMW", "Lamborghini"]
# printing the lists
print("cars: ", cars)
print("more: ", more)
# extending the cars i.e. inserting
# the elements of more list into the list cars
cars.extend(more)
# printing the list after extending
print("cars: ", cars)
Output
输出量
cars: ['Porsche', 'Audi', 'Lexus', 'Audi']
more: ['Porsche', 'BMW', 'Lamborghini']
cars: ['Porsche', 'Audi', 'Lexus', 'Audi', 'Porsche', 'BMW', 'Lamborghini']
Example 2:
范例2:
# Python List extend() Method with Example
# declaring the list
x = [10, 20, 30]
print("x: ", x)
# inserting list elements and printing
x.extend([40, 50, 60])
print("x: ", x)
# inserting set elements and printing
x.extend({
70, 80, 90})
print("x: ", x)
Output
输出量
x: [10, 20, 30]
x: [10, 20, 30, 40, 50, 60]
x: [10, 20, 30, 40, 50, 60, 80, 90, 70]
翻译自: https://www.includehelp.com/python/list-extend-method-with-example.aspx
extend python
还没有评论,来说两句吧...