Python program to remove an element from a list using ‘del’ statement
Python 3 program to remove an element from a list using ‘del’ statement :
All items are placed inside a square bracket in a python list. The index of the items starts from 0, i.e. the index of the first element is 0, the index of the second element is 1 etc. We can access any item in a list using its index. Also, the python list is mutable. We can remove or change any item of a list using its index. In this post, I will show you how to remove single or multiple elements from a python list.
Python comes with a lot of inbuilt functions to make our life easier. For deleting an item in a list, we have one statement called ‘del’. This statement can be used to remove one or multiple elements from a list. In this example, we will learn different uses of ‘del’ statement.
Let’s take a look :
Delete a single element from a list :
Using the ‘del‘ statement, we can delete one single element from a list by using its index. If the name of the list is ‘list‘ and if we want to remove an element in position ‘ i’, we can use ‘del list[i]‘. Index starts from ‘0‘. Example :
So, we have removed the element in 5th position, i.e. ‘6‘.
The index of the items starting from the deleted element is changed after this modification. In this example, the index of 7 was 6 previously. After the deletion, its index became 5. Similarly, the index of 8 and 9 is also changed. This point should be kept in mind if you are deleting an item in a list.
Delete multiple elements from a list :
For deleting multiple elements, we can use one loop and delete the items one by one using the above approach.
But ‘del’ statement already provides this functionality to delete items within a given index range. We need to define the start and end index and it will delete all items starting from the start index to the end index, excluding the end indexed element. e.g., ‘del list[min_range:max_range]’ will delete elements of the list from index position ‘min_range’ to ‘max_range – 1’. Example :
The above program removes the elements from index number 2 to index 4. Note that it excludes the end index item.
If we want to delete only specific indexed elements from a list, like index 1, 3, 5, and 6, we will have to use one loop. Using a for loop and by using the del statement, we can delete any number of items one by one in a list.
Deleting all elements from a list :
To delete all elements of a list, we can use ‘del list_name[:]’. It is similar to the above process of deleting multiple elements. The only difference is that we are not passing any start and end index here. Example :
So, all the elements of the list are removed ‘del my_list[:]‘.
Remove list variable using ‘del’ :
The above process will delete all elements from a list, but it will not delete the list variable. We can also remove a variable using ‘del’. ‘del list_name’ to remove list variable with name ‘list_name’. Example :
Similar tutorials :
- How to find the intersection of two list elements in python
- Python flatten a nested list or list of lists
- Find the product of all odd, even numbers in a python list
- Python program to remove one occurrence of a word in a list
- Python examples to create list of objects
- Python program to print list elements in different ways