Python append()

python 中的append()函数有助于将给定的项目添加到现有列表的末尾。

 **list.append(item)** #where item can be numbers, strings, dictionaries etc 

追加()参数:

append()函数接受一个参数。

参数描述必需/可选
项目要添加到列表末尾的项目需要

追加()返回值

这个方法不返回任何值,这意味着它不返回任何值。实际上,这个方法更新了现有的列表。

Python 中append()方法的示例

示例append()在 python 中是如何工作的?

 # flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# 'Sunflower' is appended to the flowers list
flowers.append('Sunflower')

# Updated flowers list
print('Updated flowers list: ', flowers ) 

输出:

 Updated flowers list:  ['Rose', 'Lotus', 'Jasmine', 'Sunflower'] 

示例 2:如何使用append()向列表中添加列表

 # flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# flowers list
flowers = ['Rose', 'Lotus', 'Jasmine']

# list of vegetables 
vegetables = ['Tomato', 'Potato']

# appending vegetables list to the flowers list
flowers.append(vegetables)

print('Updated list: ', flowers) 

输出:

 Updated list:  ['Rose', 'Lotus', 'Jasmine', ['Tomato', 'Potato']] 

原创文章,作者:Z4F83,如若转载,请注明出处:https://www.506064.com/n/127326.html

(0)
Z4F83Z4F83
上一篇 2024-10-03
下一篇 2024-10-03

相关推荐

发表回复

登录后才能评论