How to remove an item from a list in python?

Python supports various inbuilt methods to remove items from the list such as remove(), pop(), or a del statement. The remove() method will delete a specified item from the list. The pop() method and the del statement are used to remove an item from a specified index.

Removing an item from the list using the remove() method

An item can be removed from the list by passing the item as an argument to the remove() function. The remove() method removes the first matching element from the list. It accepts a single argument and doesn't return any value.

For example, let us consider a fruits list fruit_list = ["apple" , "orange" ,"grapes"]. The item "orange" is passed as an argument to remove() function. The fruit_list is now updated to ["apple" ,"grapes"].

fruit_list = ["apple" , "orange" ,"grapes"]
fruit_list.remove("orange")
print("The updated list is: ", fruit_list)

Output

The updated list is:  ['apple', 'grapes']
If an item passed as an argument to the remove() function is not in the list it raises a ValueError exception.

For the above example, if "guava" is passed as an argument the program throws a ValueError exception.

fruit_list = ["apple" , "orange" ,"grapes"]
fruit_list.remove("guava")
print("The updated list is: ", fruit_list)

Output

fruit_list.remove("guava")
ValueError: list.remove(x): x not in list

Removing an item from the list using the pop() method

An item at a specified index can be removed by using the pop() method. The index is passed as an argument to the pop() function. The pop() function accepts a single argument and if a parameter is not passed, by default -1 is passed to the pop() function. The pop() function returns the value being removed at a given index.

For example, let us consider a fruits list fruit_list = ["apple" , "orange" ,"grapes"]. The index "1" is passed as an argument to pop() function. The fruit_list is now updated to ["apple" ,"grapes"].

fruit_list = ["apple" , "orange" ,"grapes"]
item = fruit_list.pop(1)
print("The removed item is:",item)
print("The updated list is: ", fruit_list)

Output

The removed item is: orange
The updated list is:  ['apple', 'grapes']
If the index passed to pop() function is not in the range then it raises an IndexError exception.

For the above example, if 3 is passed as an argument the program throws an IndexError exception.

fruit_list = ["apple" , "orange" ,"grapes"]
item = fruit_list.pop(3)
print("The removed item is:",item)
print("The updated list is: ", fruit_list)

Output

 item = fruit_list.pop(3)
IndexError: pop index out of range

Removing an item using del statement

The del statement can be used to remove an item from a specified index. The syntax for the delete statement is as follows:

syntax:
del object

Here the object can be variables, lists, tuples, sets, dictionaries, user-defined objects, items in list or tuples, etc.

For example, let us consider a fruits list fruit_list = ["apple" , "orange" ,"grapes"]. The item "orange" is present at index 1. The delete statement del fruit_list[1] will remove the item from fruit_list. The fruit_list is now updated to ["apple" , "grapes" ].

fruit_list = ["apple" , "orange" ,"grapes"]
del fruit_list[1]
print("The updated list is: ", fruit_list)

Output

The updated list is:  ['apple', 'grapes']
If we try to delete an item at an index that is not in the range it raises an IndexError exception.

For the above example, if we try to delete an item at index 3 which is out of range the program throws an IndexError exception.

fruit_list = ["apple" , "orange" ,"grapes"]
del fruit_list[3]
print("The updated list is: ", fruit_list)

Output

del fruit_list[3]
IndexError: list assignment index out of range


0 results
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions