How to Remove an element from a List Using Index in Python – Definitive Guide

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.

If You’re in Hurry…

As demonstrated below, you can use the pop() function to remove an item using its index.

colours = ['Orange', 'White', 'Green', 'Blue']

colours.pop(0)

If You Want to Understand Details, Read on…

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.

Using Pop

Pop() method is used to remove 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.

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'

You can use the pop() method when you want to delete an item at a specific index and have the actual value as the return value.

Using Del

del statement is used to remove an element from a list using its index.

You need to 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.

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.

  1. Accept the list of indexes
  2. Sort it in the descending order using reverse=True
  3. Iterate the sorted list of indexes and delete the item from that specific index using pop().

Code

The below 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 below 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'

Conclusion

You’ve learned how to remove an element from a list using index in Python.

Additionally, you’ve learned about creating a user-defined function for deleting multiple items at once using its index.

You May Also Like

Leave a Comment