del,remove,pop有什么区别

_l = ['Mecell', 15, 'Python', True, None, [1, 2, 3], 'Python']

1. del——指定索引值删除

# del 列表[索引值]
del _l[1]
    
# 源列表:
['Mecell', 15, 'Python', True, None, [1, 2, 3], 'Python']
 
# del删除数据后的列表:
['Mecell', 'Python', True, None, [1, 2, 3], 'Python'] 

2. remove——默认移除第一个出现的元素

# 列表.remove[删除对象]
# 对象可以是列表里面的任何数据类型:字符串、数字、bool等
a_list.remove['Python']
 
# 源列表:
['Mecell', 15, 'Python', True, None, [1, 2, 3], 'Python']
# remove删除数据后的列表:
['Mecell', 15, True, None, [1, 2, 3], 'Python']

#列表里面有两个'Python',但是实际上只是删除了第一个,最后一个并没有删除,这就是remove的特点

3. pop——括号内不添加索引值,则默认删除列表中的最后一个元素;反之则默认根据索引值删除 .有返回值

# 列表.pop() --删除最后一个元素
a_list.pop()
 
# 源列表:
['Mecell', 15, 'Python', True, None, [1, 2, 3], 'Python']
# pop删除数据后的列表:
['Mecell', 15, 'Python', True, None, [1, 2, 3]]
 
 
# 列表.pop(索引值) --指定索引值删除
a_list.pop(3)
# 源列表:
['Mecell', 15, 'Python', True, None, [1, 2, 3], 'Python']
# pop删除数据后的列表:
['Mecell', 15, 'Python', None, [1, 2, 3], 'Python']

 

posted @ 2021-02-24 17:33  书生_fox  阅读(1040)  评论(0)    收藏  举报