Delete Last Element
来源:互联网 发布:java springmvc 分页 编辑:程序博客网 时间:2024/06/05 08:50
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
add a comment
2 Answers
activeoldestvotes
up vote35down vote
you can use lst.pop()
or del lst[-1]
that pop() method rocks, thanks – armani Nov 18 '14 at 22:49
pop()
removes and returns the item, in case you don't want have a return use del
;) – flacle 2 days agoadd 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]
0 0
- Delete Last Element
- Remove first and last element
- sql server CANNOT DELETE LAST ...
- JS array delete element
- angular ng-repeat set last element style
- LintCode Delete Element 删除元素
- delete the element of array
- Html Dom add and delete element
- 1、Find the last element of a list.
- 关于ERWIN “Cannot DELETE last xxx because xxx exists.”
- last
- last
- last
- last
- last
- 1. Delete the element accidently, fail to add
- Find the nth to last element of a singly linked list
- 【10】Find nth to last element of a singly linked list
- LIS+输出路径模板(1160)
- android学习笔记之-xml pull解析
- Light OJ 1188 Fast Queries(离线树状数组||莫队算法)
- nodejsPath
- uva10453
- Delete Last Element
- 搭建自己的MVC框架
- 杨鑫的C++上机报告
- [GDKOI2016]魔卡少女
- win8.1系统中Sybase PowerBuilder已停止工作的有效解决办法
- mysql 常见错误:Can't create table... errno150原因分析
- PAT-A1009 Product of Polynomials (25)(模拟)
- Mac的MySQL无法启动的原因
- 基数排序
clearLists
? – Wooble Dec 2 '11 at 15:07