W3docs

How to remove an element from a list by index

To remove an element from a list by index in Python, you can use the pop() method.

To remove an element from a list by index in Python, you can use the pop() method. The pop() method removes an element at a specific index and returns the element that was removed.

Here's an example:

Remove an element from a list by index using pop method in Python

# Initialize the list
my_list = [1, 2, 3, 4, 5]

# Remove the element at index 2 (which is 3)
removed_element = my_list.pop(2)

# Print the list and the removed element
print(my_list)   # [1, 2, 4, 5]
print(removed_element)   # 3

<div class="alert alert-info flex not-prose"> Watch a course <span class="hidden md:block">Watch a video course </span> Python - The Practical Guide</div>

You can also use the del statement to remove an element from a list by index.

Here's an example:

Remove an element from a list by index using a del statement in Python

# Initialize the list
my_list = [1, 2, 3, 4, 5]

# Remove the element at index 2 (which is 3)
del my_list[2]

# Print the list
print(my_list)   # [1, 2, 4, 5]

Note that the del statement does not return the removed element, it simply removes it from the list.