Python List remove()

Python list remove() | In this post, we will discuss how to remove items or elements from the list using the remove() method. The remove() is an inbuilt function in Python that searches for the given element in the list and removes the first matching element.

The Syntax of the remove() method is:

list_name.remove(element)

Parameters:

The remove() method takes a single object as an argument and object to be removed from the list.

Return Value:

The remove() method does not return any value (returns None) but removes the given object from the list.

Python Remove From List

We will make a list while declaring the variables then, the Python program removes elements from the list. Finally, the new list will be displayed on the screen.

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'HTML', 'Javascript']

# printing original list
print('List:', my_list)

# removed HTML from the list
my_list.remove('HTML')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘HTML’, ‘Javascript’]
New list: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]

If the given element does not exist in the list, remove() method are getting ValueError: list.remove(x): x not in list exception.

# Python program to remove item from list

# take list
my_list = ['C', 'Java', 'Python', 'Javascript']

# printing original list
print('List:', my_list)

# removed HTML from the list
my_list.remove('HTML')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Javascript’]
Traceback (most recent call last):
File “main.py”, line 10, in
my_list.remove(‘HTML’)
ValueError: list.remove(x): x not in list

Here, we are getting an error because the HTML does not contain in the list.

Python Remove Duplicates from List

In the previous program, the list contains a unique element but in this program, we are getting a list that contains duplicate elements, then the remove() method only removes the first matching element.

# Python program to duplicates item from list

# take list
my_list = ['C', 'Java', 'Python', 'Java', 'Javascript', 'Java']

# printing original list
print('List:', my_list)

# removed Java from the list
my_list.remove('Java')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]
New list: [‘C’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]

Remove all ‘Java’ from the list and print the list:

# Python program to duplicates item from list

# take list
my_list = ['C', 'Java', 'Python', 'Java', 'Javascript', 'Java']

# printing original list
print('List:', my_list)

# removed all Java from the list
while (my_list.count('Java')):
    my_list.remove('Java')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]
New list: [‘C’, ‘Python’, ‘Javascript’]

Other Method:

# Python program to duplicates item from list

# take list
my_list = ['C', 'Java', 'Python', 'Java', 'Javascript', 'Java']

# printing original list
print('List:', my_list)

# removed all Java from the list
while 'Java' in my_list: my_list.remove('Java')

# print list after item deletion
print('New list:', my_list)

Output:-

List: [‘C’, ‘Java’, ‘Python’, ‘Java’, ‘Javascript’, ‘Java’]
New list: [‘C’, ‘Python’, ‘Javascript’]

Also See:- Remove the Last Element from List in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *