Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Delete items from Sorted list
Deleting items from a Sorted list:
We can delete items from sorted listed in Python using the following methods
- Pop- delete items based on index of elements
- Remove- removes the particular item
- Del- deletes group of elements
- Clear- clears an entire list
We can use any of these according to the application. Let’s see how each function works for a sorted list and lastly, we will see a method to delete an item manually.
- Pop deletes based on index of the items. If index not given then it take last index of list in default . We can store the deleted item into another variable.
Popped element: 5
After popping element list- [1, 2, 4]
- Pop operation Takes O(1) time, to delete the element from the list but it deletes the last element from the list.
- Remove deletes the item that is passed as argument for remove function. It removes first occurrence of that item.
After Removing a element from list- [1, 2, 5]
- Remove Takes O(n) time to delete a item from the list.
- Clear is used to delete entire list at same time.
- Del() function deletes group of elements of list at once by using slicing. del(x[:]) this will delete entire list
We can delete a item from list manually .
List after deleting group of elements: [1, 3, 3, 22, 56]
After deleting a element : [1, 3, 3, 56]
After clear function: 
- These are the various method to delete items from sorted list in python.