How to delete last item in list?

Submitted 3 years, 6 months ago
Ticket #248
Views 219
Language/Framework Python
Priority Low
Status Closed

I have this program that calculates the time taken to answer a specific question, and quits out of the while loop when answer is incorrect, but i want to delete the last calculation, so i can call min().

from time import time

q = input('What do you want to type? ')
a = ' '
record = []
while a != '':
    start = time()
    a = input('Type: ')
    end = time()
    v = end-start
    record.append(v)
    if a == q:
        print('Time taken to type name: {:.2f}'.format(v))
    else:
        break
for i in record:
    print('{:.2f} seconds.'.format(i))
Submitted on Oct 19, 20
add a comment

1 Answer

Verified

You can use the slicing notation By keeping everything except the last item:

record = record[:-1]

But a better way is to delete the item directly:

del record[-1]

Submitted 3 years, 5 months ago


Latest Blogs