There is a list, for example,
有一个列表,例如,
a=[1,2,3,4]
I can use
我可以用
a.append(some_value)
to add element at the end of list, and
在列表末尾添加元素,和
a.insert(exact_position, some_value)
to insert element on any other position in list but not at the end as
在列表中的任何其他位置插入元素,但不在末尾插入元素
a.insert(-1, 5)
will return [1,2,3,5,4]. So how to add an element to the end of list using list.insert(position, value)?
将返回[1,2,3,5,4]。那么如何使用list.insert(position,value)将元素添加到列表末尾?
1 个解决方案
#1
33
You'll have to pass the new ordinal position to insert
using len
in this case:
在这种情况下,您必须传递新的序号位置以使用len插入:
In [62]:
a=[1,2,3,4]
a.insert(len(a),5)
a
Out[62]:
[1, 2, 3, 4, 5]
#1
33
You'll have to pass the new ordinal position to insert
using len
in this case:
在这种情况下,您必须传递新的序号位置以使用len插入:
In [62]:
a=[1,2,3,4]
a.insert(len(a),5)
a
Out[62]:
[1, 2, 3, 4, 5]