In this tutorial, learn how to remove list elements using Python. Delete list element using various Python functions with examples given. You can also remove all the elements from the Python list.

There is various function 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 the only single element from the list.

Use the list variable concatenated with the remove() to remove the list element. See the example given below 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.

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 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 de(). 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.

Hope, you like this post of how to remove the list element using Python. If you have any query regarding the tutorial, please comment below.

Also tell me, which method you are using to delete any elements from the Python list.

Leave a Reply

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

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