CBSE Class 12 Computer Science
Question 59 of 105
Python Revision Tour II — Question 8
Back to all questionsThe two ways to remove something from a list are:
pop method — The syntax of pop method is
List.pop(<index>).del statement — The syntax of del statement is
del list[<index>] # to remove element at index
del list[<start>:<stop>] # to remove elements in list slice
Difference
The difference between the pop() and del is that pop() method is used to remove single item from the list, not list slices whereas del statement is used to remove an individual item, or to remove all items identified by a slice.
Example
pop() method:
t1 = ['k', 'a', 'e', 'i', 'p', 'q', 'u']
ele1 = t1.pop(0)
print(ele1)Output — 'k'
del statement:
lst = [1, 2, 3, 4, 5]
del lst[2:4]
print(lst)Output — [1, 2, 5]