Remove element from list Python by index | Example code

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:

Remove element from list Python by index

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.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.