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/zh-hk/n/127326.html