Remove an element from a python list (pop, del, clear, remove)
In this post, you will learn several methods to delete or remove elements from the python list. method like pop(), clear(), remove() or del keyword.
Pop()
: remove elements by their indexDel
: remove element by index or remove multiple elementsClear()
: remove all elements from the listRemove()
: remove an element by its value.- By using the
list comprehension
method
Remove elements by their index by using the pop() method
Pop() is an inbuilt method, you can delete an element from a list by the passing index
of the element. Before passing the index first check whether an index is valid or not, if an index is not valid then pop()
method will raise IndexError
.
If you don’t pass the index position in the pop() method then it will remove the last element by default.
syntax of pop():
list1=[0,1,2,3,4]
list1.pop[index]
always remember index position is start from 0. So if there are 5 elements in a list then the index will be 0 to 4
.
my_list=['start','learn', 'study', 'program', 'code',5, 'python', 2]
print("before removing list:",my_list)
element=my_list.pop(2)
print("deleted element is:",element)
print("After removing element list become",my_list)
#delete a range of element by using pop()
#by using for loop
my_list1=['st', 'learn', 'sty', 'pr', 'code', 'strr', 'pyth', 20]
print("before deleting list:",my_list1)
delete=[1, 3, 4, 6] # all index which you want to delete
for i in delete:
my_list1.pop(2)
print("list after deleting range of element",my_list1)
output
before removing list: ['start', 'learn', 'study', 'program', 'code', 5, 'python', 2] deleted element is: study after removing element list become ['start', 'learn', 'program', 'code', 5, 'python', 2] before deleting list: ['st', 'learn', 'sty', 'pr', 'code', 'strr', 'pyth', 20] list after deleting range of element ['st', 'learn', 'pyth', 20]
Use of del for removing item by index
Del
is another way to delete an element from a list by index, but by using del you can delete a range
of elements
Delete by index
list1=[1,2,3,4]
del list1[0]
print("list1 after delete:",list1)
#output:- list1 after delete: [2, 3, 4]
By using the del
keyword, you can delete multiple elements.
Delete multiple elements
# delete range of element
list1=[1,2,3,4]
del list1[1:3]
print("After removing multiple items", list1)
#output:-After removing multiple items [1, 4]
Remove all elements in the list by using clear() method
clear()
method doesn't remove individuals element, it removes all elements present in the list and makes it an empty list without deleting the list variable.
see the below example of how the clear() method removes all elements.
Example
my_list=[1, 2, 'dam', 4, 5, 6, 7, 8, 9]
my_list.clear()
print(my_list)
#output:- []
Remove a element by value: remove()
remove()
method is used to delete the first occurrence of numbers or strings mentioned in its arguments.
Example
list1=['bob', 'jack', 'alice', 'robato', 'top']
list1.remove('bob')
print(list1)
#output:- ['jack', 'alice', 'robato', 'top']
If the list contains more than one specified value in an argument then only the first occurrence of the specified value gets deleted.
In the below example alice
contains two times in a list but only the first occurrence is deleted.
Example
list1=['alice', 'jack', 'bob', 'alice', 'robato', 'rain']
list1.remove('alice')
print(list1)
#output:- ['jack', 'bob', 'alice', 'robato', 'rain']
if the specified value in an argument is not available in a list then it will raise ValueError
.
Example
list1=['all', 'albert', 'jack', 'bob', 'first', 'rain']
list1.remove('rabato')
print(list1)
#output:- ValueError: list.remove(x): x not in list
remove elements by using list comprehension
List comprehensions
are used for creating a new list based on the value of an existing list. It basically filters
your old list and makes a new list. For example, you can separate all odd elements from a list by using List comprehensions.
Example
list1 = list(range(10))
print(list1)
#output:-[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
lc=[i for i in list1 if i % 2 != 0]
print("odd value",lc)
#output:-odd value [1, 3, 5, 7, 9]