This Python function is used to remove the specified user item from an existing list. In this section, we discuss how to use this method to remove the first matching list items with practical examples.
The syntax of this Python List remove to delete the first matching is:
Name.remove(Item or Value)
Python list remove function on integers
The below code will delete 75, 45, and 10 elements from numbers.
numbers = [10, 15, 25, 45, 65, 75, 98, 225] print("Items are : ", numbers) numbers.remove(75) print("After Del 75 are : ", numbers) numbers.remove(45) print("After Del 45 are : ", numbers) numbers.remove(10) print("After Del 10 are : ", numbers)
Items are : [10, 15, 25, 45, 65, 75, 98, 225]
After Del 75 are : [10, 15, 25, 45, 65, 98, 225]
After Del 45 are : [10, 15, 25, 65, 98, 225]
After Del 10 are : [15, 25, 65, 98, 225]
Python remove function on string list
In this example, we declared a string list. Next, we used the method to delete items such as Orange, Kiwi, and Apple.
- We are passing the Orange items from the fruits to delete and display the remaining.
- Delete the Kiwi item from the fruits. Next, we eliminated the element called Apple.
TIP: Please refer to the List and function articles in Python programming language.
fruits = ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry'] print("Items are : ", fruits) fruits.remove('Orange') print("Items after Orange: ", fruits) fruits.remove('Kiwi') print("Items after Kiwi : ", fruits) fruits.remove('Apple') print("Items after Apple: ", fruits)
Items are : ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']
Items after Orange: ['Apple', 'Grape', 'Banana', 'Kiwi', 'Cherry']
Items after Kiwi : ['Apple', 'Grape', 'Banana', 'Cherry']
Items after Apple: ['Grape', 'Banana', 'Cherry']
If we know the element or the value inside the List, then use this Python function to remove the element.
- We are removing the Grape item from Fruit1 and displaying the remaining.
- Eliminate the Orange item from the Fruit2. Our Fruits contain duplicate elements, i.e., two Orange, but this method deletes only one item. It is because this method only eliminates the first instance of the item and ignores the remaining.
- We deleted the non-existing item. That is why it is throwing an error saying x is not present.
# Grape Item from Fruit Fruit1 = ['Apple', 'Orange', 'Grape', 'Banana'] Fruit1.remove('Grape') print(Fruit1) print('========') # Orange Item from Fruits Fruit2 = ['Apple', 'Orange', 'Grape', 'Banana', 'Orange'] Fruit2.remove('Orange') print(Fruit2) print('========') # Non Existing Grape Item from Fruits1 Fruit3 = ['Apple', 'Orange', 'Banana'] Fruit3.remove('Grape') print(Fruit3)
['Apple', 'Orange', 'Banana']
========
['Apple', 'Grape', 'Banana', 'Orange']
========
Traceback (most recent call last):
File "/Users/suresh/Desktop/simple.py", line 14, in <module>
.....
.........
ValueError: x not in it
Python remove function on mixed list items
Let me use this on Mixed elements. Here, we are deleting Grape, 16, and Banana items from the fruits. Within the last statement, we are trying to eliminate 0, which doesn’t exist. That is the reason the list remove function was throwing an error.
fruitsLi = ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22] print("Items are : ", fruitsLi) fruitsLi.remove('Grape') print("Items after Deleting Grape: ", fruitsLi) fruitsLi.remove(16) print("Items after Deleting 16 : ", fruitsLi) fruitsLi.remove('Banana') print("Items after Deleting Banana: ", fruitsLi) fruitsLi.remove(0) print("Items after Deleting 0: ", fruitsLi)
Items are : ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22]
Items after Deleting Grape: ['Apple', 2, -12, 'Banana', 16, 'Kiwi', -22]
Items after Deleting 16 : ['Apple', 2, -12, 'Banana', 'Kiwi', -22]
Items after Deleting Banana: ['Apple', 2, -12, 'Kiwi', -22]
Traceback (most recent call last):
File "/Users/suresh/Desktop/Sample.py", line 14, in <module>
....
............. x not in it
This Python program removes elements from the nested list.
numbers = [[10, 22], [1, 3], [15, 25], [200, 45, 65], [75, 98, 225]] print("Available Items : ", numbers) numbers.remove([200, 45, 65]) print("Available Items after : ", numbers) numbers.remove([10, 22]) print("Available Items after : ", numbers) numbers.remove([15, 25]) print("Available Items after : ", numbers)