If I understood the question correctly, you can use the slicing notation to keep everything except the last item:
record = record[:-1]
But a better way is to delete the item directly:
del record[-1]
Note 1: Note that using record = record[:-1] does not really remove the last element, but assign the sublist to record. This makes a difference if you run it inside a function and record is a parameter. With record = record[:-1] the original list (outside the function) is unchanged, with del record[-1] or record.pop() the list is changed. (as stated by @pltrdy in the comments)
Note 2: The code could use some Python idioms. I highly recommend reading this:
Code Like a Pythonista: Idiomatic Python (via wayback machine archive).
How do I remove an element from the end of a list without returning the value? (Python)
Use the.remove() method to remove the last item from the list.
Why does remove() work slower for last elements of a list than for the first element?
why can't I remove the last element of an array and reverse it this way?
Hi, so I am writing a class for doing some methods on a list.
I already know how to add an element to the end of a list by using the append() method.
However if I want to remove an element from the end of a list, without returning the value, how would I go about that? I know the pop() method can remove an element from the end of a list but it then returns the value of that element. What kind of method can I include in my class that will remove an element from the end of the list?
So I was performing an experiment on the execution speed of the remove function on different list lengths and on three different positions of the list.
plot of running times
Green, blue and red plots denote the running times of the operation for the last element, middle element and the first element respectively.
Since remove works by shifting the subsequent elements to the left, I'd assume it'd take more time for remove to execute on the first element, k = 0, as the element shifting would be expensive. Then why is removing the last element more time consuming, by a large margin?