Python List remove() Method with Examples

In the previous article, we have discussed Python List pop() Method with Examples
List in Python:

Lists in Python are mutable sequences. They are extremely similar to tuples, except they do not have immutability constraints. Lists are often used to store collections of homogeneous things, but there is nothing stopping you from storing collections of heterogeneous items as well.

List remove() Method in Python:

The remove() method deletes the element’s first occurrence with the specified value.

Syntax:

list.remove(element)

Parameters

  • The remove() method accepts a single argument and removes it from the list.
  • If the element does not exist, a ValueError: list.remove(x): x not in list exception is thrown.

Return Value:

This function does not return any value.

Examples:

Example1:

Input:

Given List = ['hello', 'this', 'is', 'btechgeeks']
Given element to be removed = 'hello'

Output:

The given original list is: ['hello', 'this', 'is', 'btechgeeks']
The given list after removing the given element{ hello } is :
['this', 'is', 'btechgeeks']

Example2:

Input:

Given List = [-1, 0, 2, 8, 9, 2]
Given element to be removed = 2

Output:

The given original list is: [-1, 0, 2, 8, 9, 2]
The given list after removing the given element{ 2 } is :
[-1, 0, 8, 9, 2]

List remove() Method with Examples in Python

Method #1: Using Built-in Functions (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given original list.
  • Give the element to be removed as static input and store it in another variable.
  • Pass the given element as an argument to the remove() method for the given list that removes the given element from the given list.
  • Print the list after removing the given element.
  • The Exit of Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ['hello', 'this', 'is', 'btechgeeks']
# Print the given original list.
print("The given original list is:", gvn_lst)
# Give the element to be removed as static input and store it in another variable.
gvn_elemnt = 'hello'
# Pass the given element as an argument to the remove() method for the
# given list that removes the given element from the given list.
gvn_lst.remove(gvn_elemnt)
# Print the list after removing the given element.
print("The given list after removing the given element{", gvn_elemnt, "} is :")
print(gvn_lst)

Output:

The given original list is: ['hello', 'this', 'is', 'btechgeeks']
The given list after removing the given element{ hello } is :
['this', 'is', 'btechgeeks']

Method #2: Using Built-in Functions (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Print the given original list.
  • Give the element to be removed as user input using the int(input()) function and store it in another variable.
  • Pass the given element as an argument to the remove() method for the given list that removes the given element from the given list.
  • Print the list after removing the given element.
  • The Exit of Program.

Below is the implementation:

# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gvn_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Print the given original list.
print("The given original list is:", gvn_lst)
# Give the element to be removed as user input using the int(input()) function 
# and store it in another variable.
gvn_elemnt = int(input("Enter some random number = "))
# Pass the given element as an argument to the remove() method for the
# given list that removes the given element from the given list.
gvn_lst.remove(gvn_elemnt)
# Print the list after removing the given element.
print("The given list after removing the given element{", gvn_elemnt, "} is :")
print(gvn_lst)

Output:

Enter some random List Elements separated by spaces = -1 0 2 8 9
The given original list is: [-1, 0, 2, 8, 9]
Enter some random number = 2
The given list after removing the given element{ 2 } is :
[-1, 0, 8, 9]

Covered all examples on python concepts and missed checking out the Python List Method Examples then go with the available link & gain more knowledge about List method concepts in python.