Sometimes, we want to remove an element from a list by index with Python.
In this article, we’ll look at how to remove an element from a list by index with Python.
How to remove an element from a list by index with Python?
To remove an element from a list by index with Python, we can use the del operator.
For instance, we write:
a = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
del a[-1]
print(a)
We have the list a and we want to remove the last item.
To do that, we write del a[-1] to get the last item with index -1 and use the del operator to remove it.
Therefore, a is now [0, 1, 2, 3, 4, 5, 6, 7, 8] according to the print output.
Conclusion
To remove an element from a list by index with Python, we can use the del operator.