Python 列表如何添加元素

Python添加元素有三种方法:append、extend、insert

append:向列表添加元素,添加到尾部

实例:

list=["my","name","is","mark","age",18]
print("添加前:&qulinux必学的60个命令ot;,list)
list.append("test")
print("添加后:",list)
打印结果:

添加前: ['my', 'name', 'is', 'mark', 'age', 18]
添加后: ['my', 'name', 'is', 'mark', 'age', 18, 'test']

extend:将另外一个列表的元素逐一添加到指定列表中

实例:

list=["my","name","is","mark","age",18]
print(&qlinux系统安装uot;extend前:",list)
lipython怎么读st2=["A","B"]
list.extend(list2)
print("epython下载xtend后:",list)
打印结果:

extend前: ['my', 'name', 'is', 'mark', 'age', 18]
extend后: ['my', 'name', 'is', 'mark', 'age', 18, 'A', 'B']

inset(index,olinux系统bject系统运维工作内容A):在指定位置index前面插入对象objectA

实例:

list=["my","name","is","mark","age",18]
print("insertlinux前:",list)
list.insert(3,"test")
print(&python下载quot;inserpython可以做什么工作t后:",list)
打印结果:

insert前: ['my', 'name', 'is', 'mark', 'age', 18]
inslinux系统安装ert后: ['my', 'name', 'is', 'test', 'mark', 'age', 18]

add items to a list in python

how to append list in python

how to sort list in pythpython可以做什么工作on

how to use python list insertpython代码画樱花 method

pythonpython lists evepython123平台登录rything you need to know