在列表添加元素
- append()方法
append()方法在列表末尾添加元素
lists = ['a','b','c']
print(lists)
lists.append('d')
print(lists)
OUT:
['a', 'b', 'c']
['a', 'b', 'c', 'd']
- insert()方法
insert()方法在列表中指定位置插入元素
lists = ['a','b','c']
print(lists)
lists.insert(0,'d')
print(lists)
OUT:
['a', 'b', 'c']
['d', 'a', 'b', 'c']
从列表中删除元素
- del语句
del语句可删除列表中指定位置的元素
lists = ['a','b','c']
print(lists)
del lists[0]
print(lists)
OUT:
['a', 'b', 'c']
['b', 'c']
- pop()方法
pop()方法可以弹出指定位置的元素
lists = ['a','b','c']
print(lists)
element = lists.pop(0)
print(lists)
print(element)
OUT:
['a', 'b', 'c']
['b', 'c']
a
- remove()方法
remove()方法可以指定元素名进行删除
lists = ['a','b','c']
print(lists)
element = 'a'
lists.remove(element)
print(lists)
print(element)
OUT:
['a', 'b', 'c']
['b', 'c']
a
继续加油