本文共 2373 字,大约阅读时间需要 7 分钟。
python append
Python list append() method adds an element to the end of the list.
Python列表的append()方法将元素添加到列表的末尾。
The append() method adds a single item to the existing list. The original list length is increased by 1. It is one of the most popular list methods.
append()方法将单个项目添加到现有列表中。 原始列表长度增加了1。这是最流行的列表方法之一。
The append() method syntax is:
append()方法的语法为:
list.append(element)
The append() method takes a single parameter, which is appended to the end of the list. Python list is mutable.
append()方法采用单个参数,该参数将附加到列表的末尾。 Python列表是可变的。
The element can be a number, string, object, list, etc. We can store different types of elements in a list.
元素可以是数字,字符串,对象,列表等。我们可以在列表中存储不同类型的元素。
The list append() method doesn’t return anything. You can also say that the append() method returns None
.
列表append()方法不返回任何内容。 您也可以说append()方法返回None
。
Let’s look at a simple example to add an item to the end of the list.
让我们看一个简单的示例,将一个项目添加到列表的末尾。
vowels = ['a', 'e', 'i']print(f'Original List is {vowels}')vowels.append('o')vowels.append('u')print(f'Modified List is {vowels}')
Output:
输出:
Original List is ['a', 'e', 'i']Modified List is ['a', 'e', 'i', 'o', 'u']
If we pass a list to the append() method, it gets added as a single item to the end of the list.
如果我们将列表传递给append()方法,则它将作为单个项添加到列表的末尾。
list_numbers = [1, 2, 3]list_primes = [2, 3, 5, 7]list_numbers.append(list_primes)print(f'List after appending another list {list_numbers}')
Output:
输出:
List after appending another list [1, 2, 3, [2, 3, 5, 7]]
Tip: If you want to append the elements of a list to another list, use the list extend() method.
提示 :如果要将列表的元素追加到另一个列表,请使用列表extend()方法。
list_numbers_odd = [1, 3, 5]list_numbers_even = [2, 4, 6, 8]list_numbers_odd.extend(list_numbers_even)print(f'List after extending from another list {list_numbers_odd}')
Output:
输出:
List after extending from another list [1, 3, 5, 2, 4, 6, 8]
Python List append() method allows us to add any type of data to the end of the list. The method doesn’t return anything. The original list is modified and the size is increased by 1.
Python List的append()方法允许我们将任何类型的数据添加到列表的末尾。 该方法不返回任何内容。 原始列表被修改,并且大小增加了1。
翻译自:
python append
转载地址:http://pmozd.baihongyu.com/