remove() is an inbuilt function in Python programming language that removes a given object from the list. It does not return any value.
Syntax:
list_name.remove(obj)
Parameters:
obj - object to be removed from the list
Returns:
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.
Code#1:
# 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) |
Output:
[2, 1, 1, 4, 5] ['b', 'c', 'd']
Error:
It returns ValueError when the passed object is not present in the list
Code#2:
# Python3 program for the error in # remove() method # removes 'e' from list2 list2 = [ 'a', 'b', 'c', 'd' ] list2.remove('e') print(list2) |
Output:
Traceback (most recent call last):
File "/home/e35b642d8d5c06d24e9b31c7e7b9a7fa.py", line 8, in
list2.remove('e')
ValueError: list.remove(x): x not in list
Practical Application:
Given a list, remove all the 1’s from the list and print the list.
Code#3:
# 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) |
Output:
[2, 3, 4, 4, 5]
Given a list, remove all the 2’s from the list using in keyword
Code#4:
# Python3 program for practical application # of removing 2 until all 2 are removed from the list mylist = [1, 2, 3, 2, 2] # looping till all 2's are removed while 2 in mylist: mylist.remove(2) print(mylist) |
Output:
[1, 3]
Recommended Posts:
- Python | Remove all values from a list present in other list
- Python - Remove empty List from List
- Python | Convert list of string to list of list
- Python | Convert list of tuples to list of list
- Python | Convert List of String List to String List
- List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()...)
- List comprehension and ord() in Python to remove all characters other than alphabets
- Python Remove Duplicates from a List
- Python | Remove empty tuples from a list
- Python | Remove and print every third from list until it becomes empty
- Python | Remove last character in list of strings
- Python | Remove duplicate tuples from list of tuples
- Remove multiple elements from a list in Python
- Python | Ways to remove particular list element
- Python - Ways to remove duplicates from list
- Python - Remove first element of list
- Python | Remove empty strings from list of strings
- Python | Remove None values from list
- Python | Remove last K elements of list
- Python | Remove consecutive duplicates from list
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : rajinkya26

