Python remove() methodPython removes () method is a built-in method available with the list. It helps to remove the given very first element matching from the list. Show Syntax:list.remove(element)The element that you want to remove from the list. ReturnValue There is no return value for this method. Tips for using remove() method:Following are the important points to remember when using remove () method:
Example: Using remove() method to remove an element from the listHere is a sample list that i have my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya. my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove(12) # it will remove the element 12 at the start. print(my_list) my_list.remove('Riya') # will remove the first Riya from the list print(my_list) my_list.remove(100) #will throw an error print(my_list)Output: ['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback (most recent calllast): File "display.py", line 9, in <module> my_list.remove(100) ValueError: list.remove(x): x not in the listWhy use Lists?Sometimes, there may be situations where you need to handle different types of data at the same time. For example, let’s say, you need to have a string type element, an integer and a floating-point number in the same collection. Now, this is fairly impossible with the default data types that are available in other programming languages like C & C++. In simple words, if you define an array of type integer, you can only store integers in it. This is where Python has an advantage. With its list collection data type, we can store elements of different data types as a single ordered collection! Now that you know how important lists are, let’s move on to see what exactly are Lists and how to remove elements from list! Remove an element from List by value using list.remove()Python’s list provides a member function to remove an element from list i.e. It removes the first occurrence of given element from the list. For example, Suppose we have a list of numbers i.e. Let’s remove 56 from the given list using list.remove() i.e. # Remove first occurrence of 56 from List listOfnum.remove(56) It will remove the first occurrence of 56 from the above lists. Lists contents will be now, [12, 44, 45, 34, 3, 56, 4, 33, 44, 56] If we try to remove the element that doesn’t exists in list then list.remove() will throw exception. Therefore before calling list.remove() we should either, Advertisements Check if element exists in list i.e. Or use try / except i.e. # If given element doesn't exists in list, then remove() can throw Error # Therefore use try / except while calling list.remove() try : listOfnum.remove(99) except ValueError: print("Given Element Not Found in List") Related Articles
List Methods in Python | Set 2 (del, remove(), sort(), insert(), pop(), extend()…)Some of the list methods are mentioned in set 1 below List Methods in Python | Set 1 (in, not in, len(), min(), max()…) More methods are discussed in this article. 1. del[a : b] :- This method deletes all the elements in range starting from index ‘a’ till ‘b’ mentioned in arguments. 2. pop() :- This method deletes the element at the position mentioned in its arguments.
Output: List elements after deleting are : 2 1 3 8 List elements after popping are : 2 1 83. insert(a, x) :- This function inserts an element at the position mentioned in its arguments. It takes 2 arguments, position and element to be added at respective position. 4. remove() :- This function is used to delete the first occurrence of number mentioned in its arguments.
Output: 5. sort() :- This function sorts the list in increasing order. 6. reverse() :- This function reverses the elements of list.
Output: List elements after sorting are : 1 2 3 3 5 8 List elements after reversing are : 8 5 3 3 2 17. extend(b) :- This function is used to extend the list with the elements present in another list. This function takes another list as its argument. 8. clear() :- This function is used to erase all the elements of list. After this operation, list becomes empty.
Output: List elements after extending are : 2 1 3 5 6 4 3 List elements after clearing are :Related articles: This article is contributed by Manjeet Singh .If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to . 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. Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics. To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Article Tags :
Python School Programming
python-list python-list-functions Practice Tags :
python-list Python List remove()In this tutorial, we will learn about the Python List remove() method with the help of examples. The remove() method removes the first matching element (which is passed as an argument) from the list. Example# create a list prime_numbers = [2, 3, 5, 7, 9, 11]# remove 9 from the list
prime_numbers.remove(9)
# Updated prime_numbers List
print('Updated List: ', prime_numbers)
# Output: Updated List: [2, 3, 5, 7, 11] |