Remove list of elements from list Python
Ngày đăng:
09/12/2021
Trả lời:
0
Lượt xem:
131
In this article we will discuss different ways to remove an elements from list. Show Remove an element from List by value using list.remove()Pythons 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. Lets 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 doesnt exists in list then list.remove() will throw exception. Therefore before calling list.remove() we should either, 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
Remove an element from List by Index using list.pop()list.pop(index)In python lists pop() function will remove the element at given index and also returns the deleted element. If index is not given then it deletes the last element. For examples, We have a list of ints i.e. Lets remove element at index 2 # Remove the element at index 2 in list value = listOfnum.pop(2) Lists contents will be now [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] Returned value is 12 i.e. the deleted element. list.pop() and Runtime Error If list.pop() is called an index that is out of the boundary of List then it will generate a runtime error. or Use try / except # pop() can throw error if given index is out of range, so use try / except try : listOfnum.pop(20) except IndexError: print("Index Out of Range") Remove an element from List by delIn python del is a keyword and can be used to delete the element in list by index i.e. It will delete the element at index 2 in list. If del list[index] is called on an index that is out of the boundary of List then it will generate a runtime error. or Use try / except # del list[index] can throw error if given index is out of range, so use try / except try : del listOfnum[20] except IndexError: print("Index Out of Range") Complete example is as follows, def main(): # List of numbers listOfnum = [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] ''' Remove an element from List by value using list.remove() ''' print("*****list.remove()*****") print("Original List : " , listOfnum) # Remove first occurrence of 56 from List listOfnum.remove(56) print("Modified List : " , listOfnum) # 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") # Check if element exist in List, before removing if 99 in listOfnum: listOfnum.remove(99) else: print("Given Element Not Found in List") ''' Remove an element from List by Index using list.pop() ''' print("*****list.pop()*****") # List of numbers listOfnum = [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56] print("Original List : ", listOfnum) # Remove the element at index 2 in list value = listOfnum.pop(2) print("Deleted Element : ", value) print("Modified List : ", listOfnum) # Remove the last element from list value = listOfnum.pop() print("Deleted Element : ", value) print("Modified List : ", listOfnum) # pop() can throw error if given index is out of range, so check size before calling it if len(listOfnum) >= 20: listOfnum.pop(20) else: print("Index Out of Range") # pop() can throw error if given index is out of range, so use try / except try : listOfnum.pop(20) except IndexError: print("Index Out of Range") print("*****del list[index]*****") ''' Remove an element from List by del ''' listOfnum = [12, 44, 56, 45, 34, 3, 4, 33, 44] print("Original List : ", listOfnum) # Delete element at index 2 del listOfnum[2] print("Modified List : ", listOfnum) # del list[index] can throw error if given index is out of range, so check size before calling it if len(listOfnum) >= 20: del listOfnum[20] else: print("Index Out of Range") # del list[index] can throw error if given index is out of range, so use try / except try : del listOfnum[20] except IndexError: print("Index Out of Range") if __name__ == '__main__': main() Output: *****list.remove()***** Original List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] Modified List : [12, 44, 45, 34, 3, 56, 4, 33, 44, 56] Given Element Not Found in List Given Element Not Found in List *****list.pop()***** Original List : [12, 44, 12, 56, 45, 34, 3, 56, 4, 33, 44, 56] Deleted Element : 12 Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44, 56] Deleted Element : 56 Modified List : [12, 44, 56, 45, 34, 3, 56, 4, 33, 44] Index Out of Range Index Out of Range *****del list[index]***** Original List : [12, 44, 56, 45, 34, 3, 4, 33, 44] Modified List : [12, 44, 45, 34, 3, 4, 33, 44] Index Out of Range Index Out of Range Advertisements
|