Python 列表 append() 方法用於在元素的末尾追加和添加元素列表。
用法:list.append(item)
參數:
- item:要添加到列表末尾的項目
返回值:
該方法不返回任何值
示例 1:向列表中添加項目
Python
# my_list
my_list = ['geeks', 'for']
# Add 'geeks' to the list
my_list.append('geeks')
print my_list
輸出:
['geeks', 'for', 'geeks']
示例 2:將列表添加到列表中
Python
# my_list
my_list = ['geeks', 'for', 'geeks']
# another list
another_list = [6, 0, 4, 1]
# append another_list to my_list
my_list.append(another_list)
print my_list
輸出:
['geeks', 'for', 'geeks', [6, 0, 4, 1]]
注意:列表是一個對象。如果將另一個列表附加到列表中,則參數列表將是列表末尾的單個對象。
相關用法
- Python numpy.append()用法及代碼示例
- Python append() and extend()用法及代碼示例
- Python Pandas Index.append()用法及代碼示例
- Python Pandas TimedeltaIndex.append()用法及代碼示例
- Python Pandas Series.append()用法及代碼示例
- Python numpy.ma.append()用法及代碼示例
注:本文由純淨天空篩選整理自AmiyaRanjanRout大神的英文原創作品 Python List append() Method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。