python中列表的删除操作

增加:

ceshi = ['a','b','c','d','e','f']

ceshi.append('g')    
print(ceshi)    #['a','b','c','d','e','f','g'],是对原列表进行操作

在指定位置插入内容:

ceshi = ['a','b','c','d','e','f']

ceshi.insert(3,'h')
print(ceshi)   #['a', 'b', 'c', 'h', 'd', 'e', 'f']

可迭代插入:

ceshi = ['a','b','c','d','e','f']
ceshi.extend("xiaosheng")
print(ceshi)    #['a', 'b', 'c', 'd', 'e', 'f', 'x', 'i', 'a', 'o', 's', 'h', 'e', 'n', 'g']

ceshi.extend([1,2,3])
print(ceshi)   #['a', 'b', 'c', 'd', 'e', 'f', 'x', 'i', 'a', 'o', 's', 'h', 'e', 'n', 'g', 1, 2, 3]

删除元素:

pop方法(按索引删除):

ceshi = ['a','b','c','d','e','f']
del_num = ceshi.pop(1)   #如果pop括号里面不输入下标的话,默认删除最后一位
print(ceshi)    #['a', 'c', 'd', 'e', 'f']
print(del_num)   #b

remove方法(按元素删除):

ceshi = ['a','b','c','d','e','f']
ceshi.remove('a')
print(ceshi)   #['b', 'c', 'd', 'e', 'f']

清空列表:

ceshi = ['a','b','c','d','e','f']
ceshi.clear()
print(ceshi)   #[]

删除整个列表:

ceshi = ['a','b','c','d','e','f']
del ceshi

切片式删除:

ceshi = ['a','b','c','d','e','f']
del ceshi[3:]
print(ceshi)   #['a', 'b', 'c']

 

  • 7
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值