You can delete an item in list via four popular methods (including collections.deque
) :
list remove() method :
remove removes the first matching value, not a specific index
remember This method does not return any value but removes the given object from the list.
example :
list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
list_1.remove('abc')
print(list_1)
list_1.remove('total')
print(list_1)
output:
[987, 'total', 'cpython', 'abc']
[987, 'cpython', 'abc']
Second method is list del() method
You have to specify the index_no here
list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
del list_1[1]
print(list_1)
del list_1[-1:]
print(list_1)
output:
[987, 'total', 'cpython', 'abc']
[987, 'total', 'cpython']
Third one is list pop() method :
pop() removes and returns the last item in the list.
list_1 = [987, 'abc', 'total', 'cpython', 'abc'];
list_1.pop()
print(list_1)
list_1.pop()
print(list_1)
list_1.pop()
print(list_1)
output:
[987, 'abc', 'total', 'cpython']
[987, 'abc', 'total']
[987, 'abc']
Forth method is collections.deque
There are some more external module methods like :
You can pop values from both sides of the deque:
from collections import deque
d = deque()
d.append('1')
d.append('2')
d.append('3')
print(d)
d.popleft()
print(d)
d.append('1')
print(d)
d.pop()
print(d)
output:
deque(['1', '2', '3'])
deque(['2', '3'])
deque(['2', '3', '1'])
deque(['2', '3'])