Delete Last Element
2016-03-01 21:56
561 查看
List method
to delete last element in list as well
as all elements
up vote13down votefavorite 1 | yo folks, I have an issue with clearing lists. In the current program which I'm coding, I have a method that clears a certain number of lists. This is rather inconvenient since during one part of the program where this method is used, it would be a lot more helpful if it only deleted the last elementfrom the lists. Is there anyway in which I can set index numbers as parameters to my method to solve this problem? The code for the method def clearLists(self): del self.Ans[:] del self.masses[:] Whenever I want to use this method, I merely write self.ClearLists() and it deletes every element in a list. python list methods element
| ||||||||||||||||
a comment |
2 Answers
activeoldestvotesup vote35down vote | you can use lst.pop()or del lst[-1]
| ||||||||
a comment |
up vote4down vote | To delete the last element of the lists, you could use:def deleteLast(self): if len(self.Ans) > 0: del self.Ans[-1] if len(self.masses) > 0: del self.masses[-1] |
相关文章推荐
- 魔卡少女
- Android 5.0之后 点击 悬浮穿回到顶部。
- Android Studio 1.5启动出现“SDK Manager: failed to install”问题的解决
- 差分约束
- centos 6.5 samba简单配置
- nodejsPath
- 7、压缩与解压缩
- 中序遍历二叉树——递归
- NYOJ+贪心+从大到小减,是有贪心根据的额
- uva10453
- LIS+输出路径模板(1160)
- 搭建自己的MVC框架
- 安装SQL Server 2014
- 2016年3月1号【运行出错,IndentationError: unindent does not match any outer indentation level】
- jQuery 实现导航栏 lable页
- 递归思想-汉诺塔解法
- String,StringBuffer与StringBuilder的区别??
- 3ds Max2015关于注册的细节
- Android中Snackbar的使用
- ubuntu如何安装nodejs最新版 本