Which method is used to delete elements from a list if index is not known
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, inRemove 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
Python: Remove an element from a list by index using the pop() functionIn Python, the list class provides a function pop(index) to remove an item from the list at the given index. But if the list is empty or the given index is out of range, then the pop() function can raise IndexError. Therefore we should carefully use this function to delete an item from a list by index position. We have created a function for deleting an element from a list by index. It internally uses the pop() function but first checks if the given index is valid or not. Let’s understand by an example, def delete_element(list_object, pos): """Delete element from list at given index position (pos) """ if pos < len(list_object): list_object.pop(pos) list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] delete_element(list_of_num, 5) print(list_of_num)Output: Advertisements [51, 52, 53, 54, 55, 57, 58, 59]The function delete_element() accepts two arguments,
To avoid the IndexError by the pop() function, delete_element() first checks if the given index position is valid or not. If yes, then it deletes the element at the given index position by calling the pop() function. Let’s check out another example in which we are trying to delete an element by an index that is out of range, list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] delete_element(list_of_num, 15) print(list_of_num)Output: [51, 52, 53, 54, 55, 56, 57, 58, 59]It did not affect the list because, in function delete_element(), it checked if the given index is valid or not.If we use pop() function directly with this index, it will raise index error. For example, list_of_num = [51, 52, 53, 54, 55, 56, 57, 58, 59] list_of_num.pop(15)Error: list_of_num.pop(15) IndexError: pop index out of rangeList 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: List elements after inserting 4 are : 2 1 3 4 5 3 8 List elements after removing are : 2 1 4 5 3 85. 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
Read Full Article
|