How to print last deleted element from the list?

Question:

l=[1,2,3,4]
del l[0]
print(l)
#del l[0]
#want to print this number whenever it will be deleted from the list

How to print last deleted element from the list ?

Asked By: Raj

||

Answers:

The function pop returns and removes an item of the list. So you could do this instead:

l=[1,2,3,4]
print(l.pop(0))
Answered By: Joan Lara
my_list = [1, 2, 5, 8, 15, 25]

deleted_elements = []
deleted_elements.append(my_list.pop())
deleted_elements.append(my_list.pop())
deleted_elements.append(my_list.pop())

print(my_list)
print(deleted_elements)

print("Last deteleted: " + str(deleted_elements[-1]))
Answered By: User X
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.