In Python, there are many methods available on the list data type that help you remove an element from a given list. The methods are remove(), pop() and clear() . Besides the list methods, you can also use a del keyword to remove items from a list. In this Python tutorial, you will learn:

Python remove() method
Python pop() method
Python clear() method
Using del keyword
How do I remove the first element from a list?
How do I remove multiple elements from a list in Python?
How do I remove an element from a list by using an index in Python?

Example of list

The index starts from 0. In the list: my_list at 0th index we have the string ‘Guru’,

At index: 1 you will get the number 50 which is a integer. At index:2 you will get the floating number 11.50 At index:3, there is a string ‘Siya.’ At index:4, you will see the number 50 is duplicated. At index:5, you will get a list with values A, B, and C.

Python remove() method

Python removes () method is a built-in method available with the list. It helps to remove the given very first element matching from the list.

Syntax:

The element that you want to remove from the list. ReturnValue There is no return value for this method.

Tips for using remove() method:

Following are the important points to remember when using remove () method:

When the list has duplicate elements, the very first element that matches the given element will be removed from the list. If the given element is not present in the list, it will throw an error saying the element is not in the list. The remove () method does not return any value. The remove () takes the value as an argument, so the value has to pass with the correct datatype.

Example: Using remove() method to remove an element from the list

Here is a sample list that i have The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya. Output:

Python pop() method

The pop() method removes an element from the list based on the index given.

Syntax

index: the pop() method has only one argument called index.

To remove an element from the list, you need to pass the index of the element. The index starts at 0. To get the first element from the list pass index as 0. To remove the last element, you can pass the indexas -1. The index argument is optional. If not passed, the default value is considered -1, and the last element from the list is returned. If the index given is not present, or out of range, the pop() method throws an error saying IndexError: pop index.

ReturnValue: The pop() method will return the element removed based on the index given. The final list is also updated and will not have the element.

Example: Using the pop() method to remove an element from the list

The list will use in the example is my_list = [12, ‘Siya’, ‘Tiya’, 14, ‘Riya’, 12, ‘Riya’] . Let us try to remove element using a pop() method based on the following :

By giving index Without index Passing index that is out of range.

Here, we are removing Tiya from the list. The index starts from 0 , so the index for Tiya is 2. Output:

Python clear() method

The clear() method will remove all the elements present in the list.

Syntax:

Parameters: No parameters. ReturnValue: Ther is no return value. The list() is emptied using clear() method.

Example: Using clear() method to remove all elements from the list

The clear() method will empty the given list. Let us see the working of clear() in the example below: Output:

Using del keyword

To remove an element from the list, you can use the del keyword followed by a list. You have to pass the index of the element to the list. The index starts at 0.

Syntax:

You can also slice a range of elements from the list using the del keyword. The start/stop index from the list can be given to del keyword, and the elements falling in that range will be removed. The syntax is as follows:

Syntax:

Here is an example that shows to remove the first element, last element, multiple elements from the list using del. Output:

How do I remove the first element from a list?

You can make use of list methods like remove(), pop() to remove the first element from the list. In the case of remove() method, you will have to pass the first element to be removed and for pop the index, i.e., 0. You may also use the del keyword to remove the first element from the list. The example below shows to remove first element from list using remove(), pop() and del. Output:

How do I remove multiple elements from a list in Python?

The list methods remove(), and pop() are meant to remove a single element. To remove multiple aspects, make use of the del keyword. From the list [‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’], we want to remove elements B, C and D. Below example shows the how to make use of del keyword to remove the elements. Output:

How do I remove an element from a list by using index in Python?

To remove element based on index, you can make use of list method pop() . Even using del keyword will help you to remove the element for a given index. Output

Summary:

In Python, there are many methods available on the list data type that helps you to remove an element from a given list. The methods are remove(), pop() and clear(). Important built-in methods available on list to remove elements