Python添加元素有三种方法:append、extend、insert
实例:
list=["my","name","is","mark","age",18] print("添加前:",list) list.append("test") print("添加后:",list) 打印结果:
添加前: ['my', 'name', 'is', 'mark', 'age', 18] 添加后: ['my', 'name', 'is', 'mark', 'age', 18, 'test']
实例:
list=["my","name","is","mark","age",18] print("extend前:",list) list2=["A","B"] list.extend(list2) print("extend后:",list) 打印结果:
extend前: ['my', 'name', 'is', 'mark', 'age', 18] extend后: ['my', 'name', 'is', 'mark', 'age', 18, 'A', 'B']
实例:
list=["my","name","is","mark","age",18] print("insert前:",list) list.insert(3,"test") print("insert后:",list) 打印结果:
insert前: ['my', 'name', 'is', 'mark', 'age', 18] insert后: ['my', 'name', 'is', 'test', 'mark', 'age', 18]