How to remove an object from a list in Python?



To remove items (elements) from a list in Python, use the list functions clear(), pop(), and remove(). You can also delete items with the del statement by specifying a position or range with an index or slice.

In this article, we will show you how to remove an object/element from a list using python. The following are the 4 different methods to accomplish this task −

  • Using remove() method

  • Using del keyword

  • Using pop() method

  • Using clear() method

Assume we have taken a list containing some elements. We will return a resultant list after removing the given item from an input list using different methods as specified above.

Method 1: Using remove() method

The remove() function deletes the given item passed as an argument to it. −

Syntax

list.remove(element)
Return Value:The remove() function will not return any value(returns None)

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Create a variable to store the input list.

  • Use the remove() method to remove the particular item from the input list by passing the list item to be deleted as an argument to it and applying it to the input list.

  • Print the result list after removing the specified item from the input list

Example

The following program removes the specified item from an input list using the remove() method and prints the resultant list −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the 'TutorialsPoint' from the list using the remove() method inputList.remove("TutorialsPoint") # Printing the result list print("Input List after removing {TutorialsPoint}:\n", inputList)

Output

On executing, the above program will generate the following output −

Input List after removing {TutorialsPoint}:
['Python', 'Codes', 'hello', 'everyone']

As an input to the code, we were given a sample list with some random values, as well as an object/element to remove from the list. The element was then passed to the remove() method, where it was deleted/removed from the list. If the object/element is not found in the list, a value error will be returned.

Method 2: Using del Keyword

The del statement is not a List function. The del statement can be used to delete items from the list by passing the index of the item (element) to be deleted

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Use the del keyword, to remove the item present at the specified index (here 2nd index(Codes)) from the list.

  • Print the resultant list i,e after removing the specified item from the list.

Example

The following program removes the specified item from an input list using the del keyword and prints the resultant list −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the item present at the 2nd index(Codes) from the list del inputList[2] # Printing the result list print("Input List after removing the item present at the 2nd index{Codes}:\n", inputList)

Output

On executing, the above program will generate the following output −

Input List after removing the item present at the 2nd index{Codes}:
['TutorialsPoint', 'Python', 'hello', 'everyone']

Method 3: Using pop() method

With pop() method, you can delete the element at the specified position and retrieve its value.

The starting value of the index is 0 (zero-based indexing).

If no index is specified, the pop() method removes the last element from the list.

Negative values can be used to represent the position from the end. The last index is -1.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Use the pop() method, bypassing the index of a list item as an argument the to it to remove the item at the given index

  • Pass the negative index values as an argument to the pop() method, to remove the list items from the last. Here for removing the last item from the list we passed the -1 index as an argument to the pop() method.

  • Print the resultant list i,e after removing the specified item from the list.

Example

The following program removes the specified item from an input list using the pop() method and prints the resultant list −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing the 'TutorialsPoint' from the list using the pop() method del_item = inputList.pop(0) # Removing the 'everyone' from the list by passing the negative index -1 last_del_item = inputList.pop(-1) # Printing the result list print("Input List after removing {TutorialsPoint}, {everyone}:\n", inputList)

Output

Input List after removing {TutorialsPoint}, {everyone}:
['Python', 'Codes', 'hello']

Method 4: Using clear() method

The clear() method removes all items from the list. The list is still there, but it is empty.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list.

  • Use the clear() method, to remove all the items from the list.

  • Print the resultant list i,e after removing all the from the list.

Example

The following program clears or empties the complete list using the clear() function −

# input list inputList = ['TutorialsPoint', 'Python', 'Codes', 'hello', 'everyone'] # Removing all the items from the list inputList.clear() # Printing the result list print("Input List after removing all the items:", inputList)

Output

Input List after removing all the items: []

Conclusion

We learned how to remove an object/element from a list using four different methods in this article: remove(), pop(), clear(), and the del keyword. We also learned about the errors that those methods generate if the element is not in the list.


Advertisements