Use pop() method or del keyword to remove an element from list python by index. The pop() is useful to remove and keep an item from a list. Where del actually trashes the item.
Example remove an element from a list by index in Python
Simple example code.
Using pop() function
If you want to return the element you removed, use the pop() method.
Note: By default, the pop() function without any arguments removes the last item.
l = [1, 2, 3, 4, 5]
print(l.pop(2))
print(l)
Output:
Using the del keyword
if you just want to delete an element, use del.
This example uses del and specifies the index of the element you want to delete. Removing a “2” index element from a list.
l = [1, 2, 3, 4, 5]
del l[2]
print(l)
Output: [1, 2, 4, 5]
Do comment if you have any questions and suggestions on this Python list topic.
Note: IDE: PyCharm 2021.3.3 (Community Edition)
Windows 10
Python 3.10.1
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.