Each item in the Python list will have an index through which it can be accessed.
You can remove an element from a list using index in Python using the list.pop(index) statement.
Basic Example
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop(0)
Python lists are used to store multiple items in a single object. There are different methods to remove an item from a list using the index. Let’s discuss this in detail.
Table of Contents
Using Pop
The pop () method removes an item from a list using its index.
It accepts an item’s index as the parameter and removes it.
Returns
Returns the actual item deleted from the list.
Use the pop()
method when you want to delete an item at a specific index and have the deleted value as the return value.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop(0)
Output
The item in the index 0
of the list is Orange. Hence, the item is displayed after deleting it.
'Orange'
Using Del
The del statement removes an element from a list using its index.
- Pass the list name along with the index after the
del
keyword.
Returns
It doesn’t return any value. It just deletes the value from the list.
Use this method to delete an item and do not need any return value.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
del colours[0]
colours
Output
The below output is printed when you print the list using its name. It shows the item at the index 0
is removed.
['White', 'Green', 'Blue']
Removing Multiple Elements Using Index from List
There are no inbuilt methods available to remove multiple elements from the list using its index.
You need to create a method that will iterate over the list of indexes and deletes the item.
You need to be careful when deleting multiple elements using its index. Because deleting an item will change the index of the following items in the list. Hence, you always need to delete it from the end.
Create a method that will delete multiple elements using Index.
- Accept the list of indexes
- Sort it in the descending order using
reverse=True
- Iterate the sorted list of indexes and delete the item from that specific index using
pop()
.
Code
The following code demonstrates how to delete multiple elements using the index.
def delete_multiple_elements_Using_Index(lst, indices):
indices = sorted(indices, reverse=True)
for idx in indices:
if idx < len(lst):
lst.pop(idx)
colours = ['Orange', 'White', 'Green', 'Blue']
indices = [3,2]
## Calling the method to delete multiple items
delete_multiple_elements_Using_Index(colours, indices)
## Displaying the list after deleting the list
colours
Output
The below output shows that the items in the index 3
and 2
are deleted.
['Orange', 'White']
Remove First Element from List
You can use the pop()
method with index 0
to remove the first element from the list.
Code
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop(0)
Output
‘Orange’
Remove last Element from List
You can subtract one from the length of the list to remove the last element from the list.
Subtracting one is necessary because the length is calculated based on the index, and it is 0
based.
Code
The following code demonstrates how to delete the last element from the list using the length and the pop()
method.
colours = ['Orange', 'White', 'Green', 'Blue']
colours.pop(len(colours)-1)
Output
'Blue'