How to Delete or Remove List Elements in Python

In this tutorial, learn how to remove list elements using Python. The short answer is: use Python remove() to delete or remove the required list elements in Python. There are many other functions available in Python that are given here with examples to perform this task.

There are various functions available in Python to delete or remove list elements in Python. These functions are remove(), del(), pop(), and clear(). The explanations of these functions are given below with simple examples.

Mention Element to Remove List Element Using remove()

The remove() function uses the list element as the argument to delete. You have to pass only the single element which you want to delete. The function requires only one element and can delete only a single element from the list.

Use the list variable concatenated with the remove() to remove the list element. See the example given below to remove the single element from the list.

Output

[2, 4, 19, 9]

The above example showing the list elements after applying the remove(). If you don’t know the index of the element, you can use the element directly in the remove() to delete.

If you know the index of the list element, you can use the below-given example.

Bonus: download a Free Python cheat sheet that will show you 20+ most important examples to learn in Python.

How to Delete List Element Using del()

The del() deletes the list element using the given index. Each element of the list is located with a certain index assigned to them. To access each element from the list, you have to use the same index term.

If you know the index of the list element, you can delete the list element easily using the del(). Check the example given below to delete your own list elements.

Output

[2, 10, 19, 9]

The above example showing the elements if the list after using the del(). You have to give the index value enclosed within the square([]) bracket.

Remove List Element From the End Using pop()

The pop() function remove the list element from the end of the list. You have to use the below-given example to remove the last element each time you use pop().

The pop() removes the last element from the list but only the single one. You cannot remove more than one element from the list using pop().

Output

[2, 4, 10, 19]

The above example showing the list element after the deletion from the end. Each time you apply the pop(), it removes the elements from the end.

Delete All the List Elements Using clear()

In addition to all the above, sometimes you want to delete all the elements from the list. You have to use the clear() to delete all the elements from the list. The clear() requires no argument to pass. You have to just apply the clear function to the list.

Output

[]

The above showing the blank list after applying the clear(). So, these are the functions given above you can use with the examples given.

DOWNLOAD Free PYTHON CHEAT SHEET

I hope you like this post on how to remove the list element using Python.

Reference
Python Official Doc on Del with List