Python List remove Function

Python List remove function is used to remove the specified user item from an existing list. In this section, we discuss how to use this remove list items function with practical examples. The syntax of this Python List remove is:

List_Name.remove(Item or List Value)

Python List remove function Example

The below list remove code will remove 75, 45, and 10 from the numbers list.

# Python List Remove Function

numbers = [10, 15, 25, 45, 65, 75, 98, 225]

print("List Items are                    : ", numbers)

numbers.remove(75)
print("List Items after Removing 75 are  : ", numbers)

numbers.remove(45)
print("List Items after Removing 45 are  : ", numbers)

numbers.remove(10)
print("List Items after Removing 10 are  : ", numbers)

List remove function output

List Items are                    :  [10, 15, 25, 45, 65, 75, 98, 225]
List Items after Removing 75 are  :  [10, 15, 25, 45, 65, 98, 225]
List Items after Removing 45 are  :  [10, 15, 25, 65, 98, 225]
List Items after Removing 10 are  :  [15, 25, 65, 98, 225]

TIP: Please refer to List and List function articles in Python.

Python List remove function Example 2

In this example, we declared a string list. Next, we used remove function to delete or remove items such as Orange, Kiwi, and Apple

# Python List Remove Function

fruits = ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']

print("List Items are : ", fruits)

fruits.remove('Orange')
print("List Items after Removing Orange: ", fruits)

fruits.remove('Kiwi')
print("List Items after Removing Kiwi  : ", fruits)

fruits.remove('Apple')
print("List Items after Removing  Apple: ", fruits)

List remove function output

List Items are :  ['Apple', 'Grape', 'Orange', 'Banana', 'Kiwi', 'Cherry']
List Items after Removing Orange:  ['Apple', 'Grape', 'Banana', 'Kiwi', 'Cherry']
List Items after Removing Kiwi  :  ['Apple', 'Grape', 'Banana', 'Cherry']
List Items after Removing  Apple:  ['Grape', 'Banana', 'Cherry']

List remove function analysis:

  1. We are removing the Orange item from the fruits and displaying the remaining list.
  2. Removed the Kiwi item from the fruits. Next, we removed the Apple fruit.

remove List Items Example 3

Let me use this remove function on Mixed List. Here, we are removing Grape, 16, and Banana list items from the list of the fruits. Within the last statement, we are trying to remove 0, which doesn’t exist in this fruit list. That is the reason list remove function was throwing an error.

# Python List Remove Function

fruits = ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22]

print("List Items are : ", fruits)

fruits.remove('Grape')
print("List Items after Removing Grape: ", fruits)

fruits.remove(16)
print("List Items after Removing 16   : ", fruits)

fruits.remove('Banana')
print("List Items after Removing  Banana: ", fruits)

fruits.remove(0)
print("List Items after Removing  0: ", fruits)
List Items are :  ['Apple', 2, 'Grape', -12, 'Banana', 16, 'Kiwi', -22]
List Items after Removing Grape:  ['Apple', 2, -12, 'Banana', 16, 'Kiwi', -22]
List Items after Removing 16   :  ['Apple', 2, -12, 'Banana', 'Kiwi', -22]
List Items after Removing  Banana:  ['Apple', 2, -12, 'Kiwi', -22]
Traceback (most recent call last):
  File "/Users/suresh/Desktop/simple.py", line 14, in <module>
    fruits.remove(0)
ValueError: list.remove(x): x not in list

Remove List Items Example 4

This program removes list items from the nested list.

# Python List Remove Function

numbers = [[10, 22], [1, 3], [15, 25], [200, 45, 65], [75, 98, 225]]

print("List Items are       : ", numbers)

numbers.remove([200, 45, 65])
print("List Items after Removing  : ", numbers)

numbers.remove([10, 22])
print("List Items after Removing  : ", numbers)

numbers.remove([15, 25])
print("List Items after Removing  : ", numbers)
Python List Remove Function 4