python append_Python清单append()方法

python append

Python list append() method adds an element to the end of the list.

Python列表的append()方法将元素添加到列表的末尾。

Python清单append()语法 (Python List append() Syntax)

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.

元素可以是数字,字符串,对象,列表等。我们可以在列表中存储不同类型的元素。

列出append()返回值 (List append() return value)

The list append() method doesn’t return anything. You can also say that the append() method returns None.

列表append()方法不返回任何内容。 您也可以说append()方法返回None

Python清单append()示例 (Python List append() Example)

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']

将列表追加到另一个列表 (Appending List to another List)

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]

结论 (Conclusion)

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。

参考资料 (References)

翻译自: https://www.journaldev.com/39910/python-list-append-method

python append

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值