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).
python - How to delete last item in list? - Stack Overflow
python - Removing last list element by popping - Stack Overflow
How do I remove an element from the end of a list without returning the value? (Python)
Neater way to access the last n elements in a vec?
You can use an endless range:
let vec = vec![1, 2, 3, 4, 5];
println!("Remaining: {:?}", &vec[2..]);Prints: "Remaining: [3, 4, 5]"
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=8b1a03af4ae475b294030f3d5d43b5ad
More on reddit.comvar = [3,4,5,6,2]
for x in range(len(var)):
a = var.pop(-1)
print(a)
or reverse a list
var = var[::-1]
This issue you are facing because you are trying to iterate the loop from first element and trying to remove the last element of the list. at one pint for loop runs out of element in a list hence it stops and you don't get empty list.
The proper solution will be to reverse iterate through the list and remove the elements.
Sample Code :
A = [3,4,5,6,2]
for i in range( len(A) -1 , -1, -1):
A.pop()
print (A)