How to delete last item in list?
You need:
record = record[:-1]
before the for
loop.
This will set record
to the current record
list but without the last item. You may, depending on your needs, want to ensure the list isn't empty before doing this.
you should use this
del record[-1]
The problem with
record = record[:-1]
Is that it makes a copy of the list every time you remove an item, so isn't very efficient
list.pop()
removes and returns the last element of the list.
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).