How to Remove List Element by Index in Python

Muhammad Maisam Abbas Feb 02, 2024
  1. Remove List Element by Index With del Keyword in Python
  2. Remove List Element by Index With pop() Function in Python
How to Remove List Element by Index in Python

In this tutorial, we will discuss methods to remove list elements by index in Python.

Remove List Element by Index With del Keyword in Python

The del statement is used to delete objects in Python. The del statement can also be used to remove list elements by index. The following code example shows us how we can remove list elements by index with the del keyword in Python.

list1 = [0, 1, 2, 3, 4]

del list1[1]

print(list1)

Output:

[0, 2, 3, 4]

In the above code, we first initialize a list and then remove the element at index 1 of the list using the del keyword.

Remove List Element by Index With pop() Function in Python

The pop() function is used to remove the list element at a specified index. The pop() function returns the removed element. The following code example shows us how we can remove list elements by index with the pop() function in Python.

list1 = [0, 1, 2, 3, 4]

removedElement = list1.pop(1)

print(list1)
print(removedElement)

Output:

[0, 2, 3, 4]
1

In the above code, we first initialize a list and then remove the element at index 1 of the list using the pop() function.

Both del keyword method and pop() function perform the same task. The only difference is that the del keyword deletes the element at the given index, but the pop() function also returns the deleted element.

Muhammad Maisam Abbas avatar Muhammad Maisam Abbas avatar

Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.

LinkedIn

Related Article - Python List