Categories
Python Answers

How to remove an element from a list by index with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *