I have an issue with clearing list
s. In the current program, I have a method that clears a certain number of list
s. 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 element from the lists. Is there any way 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.
To delete the last element of the lists, you could use:
def deleteLast(self):
if self.Ans:
del self.Ans[-1]
if self.masses:
del self.masses[-1]
To delete the last element from the list just do this.
a = [1,2,3,4,5]
a = a[:-1]
#Output [1,2,3,4]
you can use lst.pop()
or del lst[-1]
pop()
removes and returns the item, in case you don't want have a return use del
Source: Stackoverflow.com