python中append
Python list supports variety of built-ins functions in which append() and insert() are two functions
Python列表支持各种内置函数,其中append()和insert()是两个函数
1) Python append() method
1)Python append()方法
append() method is used to append values at the last position of the list that is every time you append a value it will directly go to the last of the list.
append()方法用于在列表的最后一个位置附加值,这是每次您添加一个值时它将直接转到列表的最后一个位置。
append() method takes only one argument.
append()方法仅接受一个参数。
Syntax:
句法:
list_name.append( any value )
Python insert() method
Python insert()方法
insert() method is also used to insert values in a list but the advantage of insert() method over append() method is that with the help of insert() method one can insert the values at any particular index (index should be less than the size of the list).
insert()方法也可用于在列表中插入值,但是insert()方法相对于append()方法的优势在于,借助insert()方法,可以在任何特定索引处插入值(索引应小于超过列表的大小)。
insert() method takes 2 argument one is index and other is value.
insert()方法采用2个参数,一个是索引,另一个是值。
Syntax:
句法:
list_name.insert( index_value, any value )
Python code to demonstrate use, differences of append() and insert() methods
Python代码演示用法,append()和insert()方法的区别
lst = [1, 2, 3, 4, 5, 6]
print('Original list:',*lst)
# appending two values in the
# existing list to show the
# working of append() function
lst.append(9)
lst.append(15)
print('\nList after appending 9 and 15')
print(*lst)
# inserting value 20 at index 2
lst.insert(2,20)
# inserting string literal 'include'
# at index 6
lst.insert(6,'include')
print('\nList after inserting 20 and "include"')
print(*lst)
print()
Output
输出量
Original list: 1 2 3 4 5 6
List after appending 9 and 15
1 2 3 4 5 6 9 15
List after inserting 20 and "include"
1 2 20 3 4 5 include 6 9 15
翻译自: https://www.includehelp.com/python/list-append-and-insert-methods-in-python.aspx
python中append