Deletion of List Elements
To Delete one or more elements, i.e. remove an element, many built-in Python list functions can be used, such as pop() and remove() and keywords such as del.
1. Python pop() Method
Removes an item from a specific index in a list.
Syntax: list.pop([index])
The index is not a necessary parameter, if not mentioned takes the last index.
Note: The index must be in the range of the List, elsewise IndexErrors occur.
Example 1:
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop())
Output
2.5
Example 2:
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
print(List.pop(0))
Output
2.3
2. Python del() Method
Deletes an element from the list using it’s index.
Syntax: del list.[index]
Example:
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
del List[0]
print(List)
Output
[4.445, 3, 5.33, 1.054, 2.5]
3. Python remove() Method
Removes a specific element using it’s value/name.
Syntax: list.remove(element)
Example :
List = [2.3, 4.445, 3, 5.33, 1.054, 2.5]
List.remove(3)
print(List)
Output
[2.3, 4.445, 5.33, 1.054, 2.5]
We have discussed all major Python list functions, that one should know to work on list. We have seen how to add and remove elements from list and also perform basic operations like count , sort, reverse using list Python Methods.
Hope these Python methods were of help!
Python List methods
Python List Methods are the built-in methods in lists used to perform operations on Python lists/arrays.
Below, we’ve explained all the Python list methods you can use with Python lists, for example, append(), copy(), insert(), and more.