remove() is an inbuilt function in Python programming language that removes a given object from the list. It does not return any value.
obj - object to be removed from the list
The method does not return any value but removes the given object from the list.
Note:It removes the first occurrence of the object from the list.
# Python3 program to demonstrate the use of # remove() method # the first occurrence of 1 is removed from the list list1 = [ 1, 2, 1, 1, 4, 5 ] list1.remove(1) print(list1) # removes 'a' from list2 list2 = [ 'a', 'b', 'c', 'd' ] list2.remove('a') print(list2)
[2, 1, 1, 4, 5] ['b', 'c', 'd']
It returns ValueError when the passed object is not present in the list
# Python3 program for the error in # remove() method # removes 'e' from list2 list2 = [ 'a', 'b', 'c', 'd' ] list2.remove('e') print(list2)
Traceback (most recent call last): File "/home/e35b642d8d5c06d24e9b31c7e7b9a7fa.py", line 8, in list2.remove('e') ValueError: list.remove(x): x not in list
Given a list, remove all the 1’s from the list and print the list.
# Python3 program for practical application # of removing 1 untill all 1 are removed from the list list1 = [1, 2, 3, 4, 1, 1, 1, 4, 5] # looping till all 1's are removed while (list1.count(1)): list1.remove(1) print(list1)
[2, 3, 4, 4, 5]
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
- Python Dictionary copy()
- Python list | reverse()
- Python | Count the Number of matching characters in a pair of string
- Python | How and where to apply Feature Scaling?
- Testing in Python using doctest module
- Python | Simple GUI calculator using Tkinter
- Python | Program to accept the strings which contains all vowels
- Creating a simple machine learning model
- Python | Simple registration form using Tkinter
- Python | Find current weather of any city using openweathermap API