Delete Last Element

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.

share improve this question
 
2 
I don't see the relation between the question title and body. –  fortran  Dec 2 '11 at 14:54
 
Sorry about that, that was a leftover from an old, unposted question, now fixxxed. –  user1036197  Dec 2 '11 at 14:59
3 
If you don't want to clear your lists, why would you call a method called clearLists? –  Wooble  Dec 2 '11 at 15:07
1 
Am I the only what that do not understand what are you looking for? –  Tadeck  Dec 2 '11 at 15:52

2 Answers

you can use lst.pop() or del lst[-1]

share improve this answer
 
 
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 ago

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]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值