python中append_列出Python中的append()和insert()方法

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值