How to delete or remove elements from a list?
remove() – Removes an item from the list
pop() – Removes and returns an element at the given index
clear() – Removes all items from the list
del() : to remove an element. We can delete one or more items from a list using the keyword del. It can even delete the list entirely.
#remove (remove the element by value)
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
o |
m |
p |
u |
t |
e |
r |
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
Example:
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
n1.remove(‘m’)
print(n1)
#Output:
[‘c’, ‘o’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
n1.remove(‘p’)
print(n1)
#Output:
[‘c’, ‘o’, ‘u’, ‘t’, ‘e’, ‘r’]
n1.remove(‘t’)
print(n1)
#Output:
[‘c’, ‘o’, ‘u’, ‘e’, ‘r’]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
n.remove(4)
print(n)
#Output:
[0, 1, 2, 3, 5, 6, 7, 8, 9, 10]
n.remove(5)
print(n)
#Output:
[0, 1, 2, 3, 6, 7, 8, 9, 10]
n.remove(9)
print(n)
#Output:
[0, 1, 2, 3, 6, 7, 8, 10]
n.remove(10)
print(n)
#Output:
[0, 1, 2, 3, 6, 7, 8]
#n.remove(10) #error will be displayed as element is not present
print(n)
Output:
Error
Example for practice:
n=[0,1,2,3,4,5,6,7,8,9,10]
n.remove(1)
print(n)
n.remove(9)
n.remove(5)
n.remove(4)
print(n)
del (delete an element using index)
0 |
1 |
2 |
3 |
4 |
5 |
6 |
7 |
c |
o |
m |
p |
u |
t |
e |
R |
Example:
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
#Output:
[‘c’, ‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
del n1[0]
print(n1)
#Output:
[‘o’, ‘m’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
del n1[1]
print(n1)
#Output:
[‘o’, ‘p’, ‘u’, ‘t’, ‘e’, ‘r’]
Example:
n=[1,2,3,4,5,6,7,8,9,10]
del n[2]
print(n)
#Output:
[1, 2, 4, 5, 6, 7, 8, 9, 10]
del n[4]
print(n)
#Output:
[1, 2, 4, 5, 7, 8, 9, 10]
del n[7]
print(n)
#Output:
[1, 2, 4, 5, 7, 8, 9]
Example for practice (Solve):
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[1]
del n[1]
print(n)
del n[4]
del n[3]
print(n)
del n[6]
del n[2]
print(n)
Example for practice (Solve):
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[1]
del n[1]
print(n)
n=n+[10,20,30]
del n[7]
del n[9]
print(n)
n1=[5,6,7,3,4]
n=n+n1
del n[9]
del n[2]
print(n)
Delete by range:
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[0:2] #elements of position 0 and 1 are deleted
print(“n[0:2]”,n)
#Output:
n[0:2] [2, 3, 4, 5, 6, 7, 8, 9, 10]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[0:3]
print(“n[0:3]”,n) #elements of position 0,1, and 2 are deleted
#Output:
n[0:3] [3, 4, 5, 6, 7, 8, 9, 10]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[0:5]
print(“n[0:5]”,n)
#Output:
n[0:5] [5, 6, 7, 8, 9, 10]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[2:5]
print(“n[2:5]”,n)
#Output:
n[2:5] [0, 1, 5, 6, 7, 8, 9, 10]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
del n[4:8]
print(“n[4:8]”,n)
#Output:
n[4:8] [0, 1, 2, 3, 8, 9, 10]
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
del n
#delete entire list
print(n)
Output:
#error : NameError: name ‘n’ is not defined
Example for practice Solve:
n = [‘p’,’r’,’o’,’b’,’l’,’e’,’m’]
n.remove(‘p’)
print(n)
n.remove(‘l’)
n.remove(‘o’)
print(n)
Example for practice Solve:
n=[3,46,2,87,98,34,2,7,8,0,34]
del n[2]
del n[8]
print(n)
del n[2:5]
n=n+[10,20,5,67]
del n[4:6]
print(n)
del n[2:6]
print(n)
We can use remove() method to remove the given item or pop() method to remove an item at the given index.
The pop() method removes and returns the last item if index is not provided. This helps us implement lists as stacks (first in, last out data structure).
#pop (remove the element by index , if no index is given it by default removes last element)
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n.pop()
print(n)
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n.pop(0)
print(n)
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n.pop(1)
print(n)
Example:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n.pop()
n.pop()
n.pop()
print(n)
Example:
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
n1.pop()
print(n1)
Example for practice solve:
n1=[‘c’,’o’,’m’,’p’,’u’,’t’,’e’,’r’]
print(n1)
n1.pop()
n1.pop()
n1.pop()
n1.pop()
print(n1)
#Clear() method
We can also use the clear() method to empty a list.
#clear
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n.clear()
print(n)
#output
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[]
we can also delete items in a list by assigning an empty list to a slice of elements.
Example for practice Solve:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n[0:2]=[]
print(n)
Example for practice Solve:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n[0:2]=[]
print(n)
n[0:4]=[]
print(n)
Example for practice Solve:
n=[0,1,2,3,4,5,6,7,8,9,10]
print(n)
n[1:2]=[]
print(n)
n[4:6]=[]
print(n)
n=n+[10,20,30]
n[4:8]=[]
print(n)
Example for practice Solve:
n = [‘p’,’r’,’o’,’b’,’l’,’e’,’m’]
n[2:3] = []
print(n)
n[2:5] = []
print(n)