How to remove elements from the List in Python

Python has a few methods to remove elements from the lists. These methods are:

Each of these is explained below with example codes so you may understand how to use and see the difference.

What is pop method?

The pop Python method removes the element at the given position by index number. If you do not specify the position, the last element will be removed. The pop() method also returns the removed element and this is one of the differences among pop and other methods.

How to use the pop method

The syntax of using the pop() method is:

list.pop([i])

  • The i is an optional parameter that specifies the index of the list item that starts at 0. The last element is removed and returned if this is not provided. This is because the default value for this parameter is -1.
  • An error will occur if the given index is not in the range.

An example of pop method with index number

An index number is given in the pop method for removing an item from the given list. The list is displayed after using the pop method along with displaying the removed item:

See online demo and code

 

Python pop

Example of pop() without index

See the code and output without giving the index in the pop method:

See online demo and code

 

Python pop no index

Using negative index number example

As mentioned earlier, the default value of the index used in the pop method is -1. See this example where -1 index value is provided:

See online demo and code

 

Python pop negative index

You see, the same list is used as in above example and it produced the same result i.e. the last element is deleted.

Python list remove method

The list remove method also deletes the specified list item from the given list. However, it does not return any value. In addition, in the remove method, you provide the value which is matched and removed from the list.

If there are multiple occurrences of the given value, it will remove only first occurrence.

Syntax of using remove method

list.remove(x)

If x is not found in the given list, an error will be raised.

An example of using remove method

The following example shows how to use the remove method. A list of web technologies is created with four items. After that, the remove method is used for deleting the jQuery from the list. The list is displayed before and after using the Python remove() method.

See online demo and code

 

Python list remove

What if a list has duplicate items?

For this example, the list is extended with duplicate items and this is followed by using the remove method. As mentioned earlier, it will only remove the first occurrence of the item. See in this example:

See online demo and code

 

Python list remove duplicate

You see, only the first occurrence of jQuery is removed while the second still exists after using the remove() method.

The del() statement

The del statement can also be used to remove one or more items from the specified list. Like the pop() method, it takes index number of the item(s) and removes from the list. Unlike the pop() method it does not return any value.

The del statement can also be used to clear range of items and even the whole list. See the examples below for learning how to do this.

Example of using del statement with index number

In this example, the del statement is used for removing the list item by index number. A list of six integer items is created and the del statement is used for deleting the fourth element from the list which index is 3.

See online demo and code

Python del

Removing multiple elements by using del()

You may provide the index range in the del statement and it will remove multiple items because of list slicing. See how you may remove multiple elements in this example:

See online demo and code

Python del list-slicing

You can see, three elements are removed out of six. The removal started from 2 to 5 index positions.

Clearing a complete list by using del

The entire list can be cleared by assigning the empty list to the slice in the del statement. Have a look:

See online demo and code

Python del list clear

You can see an empty list is displayed.