Video courses for company/skill based Preparation
Purchase mock tests for company/skill building
Deleting items from the list in Python
In python we can delete items using following methods
- Pop- delete items based on index of elements
- Remove- removes particular item
- Del- deletes group of elements
- Clear- clears entire list
We can use any of these according to the application. Lets see how each function works.
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 : channa
List after poping an element at particular index
['milk', 'bread', 'apples', 'salt', 'sugar', 'coconut', 'salt']
Remove deletes the item that is passed as argument for remove function. It removes first occurrence of that item.
List after removing an specific element
['channa', 'milk', 'bread', 'apples', 'sugar', 'coconut', 'salt']
Del() function deletes group of elements of list at once by using slicing. del(x[:]) this will delete entire list
List after deleting group of elements
[1, 56, 3, 3, 22]
Clear is used to delete entire list at same time
List after Deleting entire list