web123456

4 ways to delete elements in list list in python

Deleting elements in python list is mainly divided into the following 3 scenarios:

  • Delete according to the index position of the target element, you can use the del keyword or pop() method;
  • Delete according to the value of the element itself, and use the remove() method provided by the list (list type);
  • To delete all elements in the list, you can use the clear() method provided by the list (list type);

del: Delete elements based on index value

del is a keyword in python, specially used to perform deletion operations. It can not only delete the entire list, but also delete certain elements in the list.

del can delete individual elements in a list in the format:

  • del listname[index]

listname: indicates the list name

index: represents the index value

list = ["python",1,2,"java"]
 print(list)

 #Use positive index
 del list[2]
 print(list)

 #Use negative index
 del list[-1]
 print(list)

['python', 1, 2, 'java']
['python', 1, 'java']
['python', 1]

del can delete a continuous element in the middle, in the format:

  • del listname[start,end]

start: Start index

end: end index

list = ["python",1,2,"java"]
 print(list)
 #Use positive index
 del list[2:3]
 print(list)

 list = ["python",1,2,"java"]
 print(list)
 #Use negative index
 del list[-3:-1]
 print(list)

['python', 1, 2, 'java']
['python', 1, 'java']
['python', 1, 2, 'java']
['python', 'java']

del will delete elements from the start index to the end index, excluding elements at the end position

pop(): Delete elements based on index value

This method is used to delete elements at the specified index in the list

Syntax: (index)

listname: indicates the list name

index: represents the index value

list = ["python",1,2,"java",78,9,80,90]
print(list)
(3)
print(list)

['python', 1, 2, 'java', 78, 9, 80, 90]
['python', 1, 2, 78, 9, 80, 90]

If the index parameter is not written, the last element in the list will be deleted by default, similar to the "stack" operation in the data structure

list = ["python",1,2,"java",78,9,80,90]
print(list)
()
print(list)

['python', 1, 2, 'java', 78, 9, 80, 90]
['python', 1, 2, 'java', 78, 9, 80]

remove(): Delete according to element value

This method will delete the element itself based on the value of the element itself

list = ["python",1,2,"java",78,9,80,90,2,34,2]
 print(list)
 #First delete 2
 (2)
 print(list)
 #First delete 2
 (2)
 print(list)
 #First delete 2
 (2)
 print(list)

['python', 1, 2, 'java', 78, 9, 80, 90, 2, 34, 2]
['python', 1, 'java', 78, 9, 80, 90, 2, 34, 2]
['python', 1, 'java', 78, 9, 80, 90, 34, 2]
['python', 1, 'java', 78, 9, 80, 90, 34]

Note: The remove() method will only delete the first element with the same value as the specified value, and it must be guaranteed that the element exists, otherwise a ValueError error will be raised. Therefore, it is best to make a judgment in advance when we use remove() to delete the element.

clear(): Delete all elements

clear() is used to delete all elements of the list, that is, to clear the list

grammar:()

listname: indicates the list name

list = ["python",1,2,"java",78,9,80,90,2,34,2]
print(list)
()
print(list)

['python', 1, 2, 'java', 78, 9, 80, 90, 2, 34, 2]
[]