python中向列表中添加元素
append、追加一个元素
extend、扩展多个元素
insert、插入一个元素。
1、append
>>> test1 = ["aaa","bbb","ccc"]
>>> test1
['aaa', 'bbb', 'ccc']
>>> test1.append("ddd")
>>> test1
['aaa', 'bbb', 'ccc', 'ddd']
2、extend
>>> test1
['aaa', 'bbb', 'ccc', 'ddd']
>>> test1.extend(["eee","fff"])
>>> test1
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
3、insert
>>> test1
['aaa', 'bbb', 'ccc', 'ddd', 'eee', 'fff']
>>> test1.insert(1,1000)
>>> test1
['aaa', 1000, 'bbb', 'ccc', 'ddd', 'eee', 'fff']
末尾插入
>>> test1
['aaa', 1000, 'bbb', 'ccc', 'ddd', 'eee', 'fff']
>>> test1.insert(len(test1),2000)
>>> test1
['aaa', 1000, 'bbb', 'ccc', 'ddd', 'eee', 'fff', 2000]