I am new to Python and this is a pretty simple question.
I have a list lst and at some point an integer n calculated with 0<=n<=len(lst) , which tells me that (for this purpose) I should discard the final elements of n in the list, The statement that comes to mind to achieve this, del lst[-n:] or maybe lst[-n:]=[] (I found that in any case, adding 0 after the colon is not a good idea, since zero is not considered large, than -n in this setting). However, both options fail if n==0 , since the list is completely empty. Do I really have to insert an explicit test for zero here or bend down before writing
del lst[len(lst)-n:]
or is there something more elegant (and reliable) that can be done here?
source share