Python list 4 Ways to Delete Elements from List

  • 2021-12-12 04:40:42
  • OfStack

Directory del: Delete elements according to index value pop (): Delete element based on index value remove (): Delete based on element value clear (): Delete all elements of the list

There are three main scenarios for deleting elements in the Python list:

Delete according to the index where the target element is located, using the del keyword or the pop () method; Delete according to the value of the element itself, using the remove () method provided by the list (list type); To remove all elements from the list, use the clear () method provided by the list (type list).

del: Delete Element Based on Index Value

del is a keyword in Python that is specifically used to perform deletion operations. It can delete not only the entire list, but also some elements in the list. We have already explained how to delete an entire list in "Python List", so this section only explains how to delete list elements.

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


del listname[index]

Where listname represents the list name and index represents the index value of the element.

del can also delete the middle 1 contiguous element in the format:


del listname[start : end]

Where start represents the starting index and end represents the ending index. del deletes elements from the index start to end, excluding elements at the end position.

[Example] Use del to delete a single list element:


lang = ["Python", "C++", "Java", "PHP", "Ruby", "MATLAB"]
# Use positive index 
del lang[2]
print(lang)
# Use negative index 
del lang[-2]
print(lang)

Run results:
['Python', 'C++', 'PHP', 'Ruby', 'MATLAB']
['Python', 'C++', 'PHP', 'MATLAB']

[Example] Delete 1 contiguous element using del:


lang = ["Python", "C++", "Java", "PHP", "Ruby", "MATLAB"]
del lang[1: 4]
print(lang)
lang.extend(["SQL", "C#", "Go"])
del lang[-5: -2]
print(lang)

Run results:
['Python', 'Ruby', 'MATLAB']
['Python', 'C#', 'Go']

pop (): Delete elements based on index values

The Python pop () method is used to delete elements at the specified index in the list in the following format:


listname.pop(index)

Where listname represents the list name and index represents the index value. If the index parameter is not written, the last element in the list will be deleted by default, similar to the "off-stack" operation in the data structure.

Examples of usage of pop ():


nums = [40, 36, 89, 2, 36, 100, 7]
nums.pop(3)
print(nums)
nums.pop()
print(nums)

Run results:
[40, 36, 89, 36, 100, 7]
[40, 36, 89, 36, 100]

Most programming languages provide a method corresponding to pop (), push (), which is used to add elements to the end of a list, similar to the "stack" operation in a data structure. However, Python is an exception, and Python does not provide the push () method, because append () can be used instead of push ().

remove (): Delete based on element value

In addition to the del keyword, Python also provides the remove () method, which deletes the element based on its own value.

Note that the remove () method removes only the first element with the same value as the specified value, and must ensure that the element exists, otherwise an ValueError error will be raised.

Example of using the remove () method:


nums = [40, 36, 89, 2, 36, 100, 7]
# No. 1 1 Secondary deletion 36
nums.remove(36)
print(nums)
# No. 1 2 Secondary deletion 36
nums.remove(36)
print(nums)
# Delete 78
nums.remove(78)
print(nums)

Run results:
[40, 89, 2, 36, 100, 7]
[40, 89, 2, 100, 7]
Traceback (most recent call last):
File "C:\Users\mozhiyan\Desktop\demo.py", line 9, in < module >
nums.remove(78)
ValueError: list.remove(x): x not in list

The last deletion, because 78 does not exist, causes an error, so we'd better judge 1 in advance when using remove () to delete elements.

clear (): Delete all elements of the list

Python clear () is used to delete all elements of the list, that is, to empty the list, see the following code:


url = list("//www.ofstack.com/python/")
url.clear()
print(url)

Run results:
[]


Related articles: