How to remove an element from a list by index in Python?

PythonServer Side ProgrammingProgramming

In this article, we will show you the remove an element from a list by index using python. Here we see 4 methods to accomplish this task −

  • Using the del keyword to remove an element from the list

  • Using the pop() function to remove an element from the list

  • Using slicing to remove an element from the list

  • Using indices to remove multiple elements from the list

Assume we have taken a list containing some elements. We will remove a specific item from a list by giving the index value using the above-specified methods.

Method 1: Using the del keyword to remove an element from the list

Following are the Algorithm/steps to be followed to perform the desired task −

Algorithm (Steps)

  • Create a variable to store the input list

  • Enter the index at which the list item is to be deleted

  • Use the del keyword, to delete the list item at the given index.

  • Print the list after deleting a specified list item.

Syntax

"del list object [index]"

It is a command that can be used to remove an item from a list based on its index location.

If the list is empty or the specified index is out of range, the del keyword will throw an IndexError.

Example

The following program returns the list after deleting the specified index list element using the del keyword −

# input list inputList = ["Welcome", "to", "tutorialspoint", "python"] # Enter the index at which the list item is to be deleted givenIndex = 3 # deleting the list item at the given index using the del keyword del inputList[givenIndex] # printing the list after deleting a specified list item print("List after deleting specified list item:", inputList)

Output

On executing, the above program will generate the following output

List after deleting specified list item: ['Welcome', 'to', 'tutorialspoint']

Method 2: Using the pop() function to remove an element from the list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Pass the given Index as an argument to the pop() function(Here pop() function removes the element of the list at the given index)

  • Print the list after deleting a specified list item.

Example

The following program returns the list after deleting the specified index list element using the pop() function −

inputList = ["Welcome", "to", "tutorialspoint", "python"] givenIndex = 1 # Passing index to the pop function to remove that element of the list at that index inputList.pop(givenIndex) # printing the list after deleting an item at the entered index print("List after deleting an item at the entered index:") print(inputList)

Output

List after deleting an item at the entered index:
['Welcome', 'tutorialspoint', 'python']

Method 3: Using slicing to remove an element from the list

Slicing can be used to create a new list by removing the item at a given index from the original input list.

Divide the list into three parts, to delete an element at index N.

  • Items in the range from 0 to N-1

  • Items at the N index position

  • Items ranging from N+1 start value and extend until the end of the list.

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task −

  • Create a variable to store the input list

  • Store the list elements before the given index and after the given index using slicing which means it removes the item at the given index.

  • Print the list after deleting a specified list item

Example

inputList = ["Welcome", "to", "tutorialspoint", "python"] givenIndex = 3 # storing the list elements before the given index and after the given index # using slicing which means it removes the item at the given index inputList = inputList[:givenIndex] + inputList[givenIndex+1:] # printing the list after deleting an item at the entered index print("List after deleting an item at the entered index:\n", inputList)

Output

List after deleting an item at the entered index:
['Welcome', 'to', 'tutorialspoint']

Method 4: Remove multiple indices elements from the list

Algorithm (Steps)

Following are the Algorithm/steps to be followed to perform the desired task

  • Create a variable to store the input list

  • Enter the indices list at which the items are to be deleted

  • Using the sorted() function(returns a sorted list of the given iterable object. If reverse=True, sorts in descending order, and reverse=False, sorts in ascending order. Default is False i.e Ascending), to sort the given indices list in descending order by passing given indices list and reverse=True as arguments.

  • Use the for loop, to traverse in the given indices list

  • Use the if conditional statement, to check whether the corresponding iterator index is less than the list length with the len() function(The number of items in an object is returned by the len() method)

  • Remove the item present at the corresponding index using pop() function if the condition is true.

  • Print the list after deleting items at the given indices.

Example

inputList = ["Welcome", "to", "tutorialspoint", "python"] # Entering the indices list at which the items are to be deleted givenIndices = [1, 3] # Reversing Indices List indicesList = sorted(givenIndices, reverse=True) # Traversing in the indices list for indx in indicesList: # checking whether the corresponding iterator index is less than the list length if indx < len(inputList): # removing element by index using pop() function inputList.pop(indx) # printing the list after deleting items at the given indices print("List after deleting items at the given indices:\n", inputList)

Output

List after deleting items at the given indices : 
['Welcome', 'tutorialspoint']

Conclusion

In this article, we learned four different ways to remove an element from a list by index. We also learned how to use the pop() and sorted() functions to remove multiple indices elements from a list.

raja
Updated on 19-Sep-2022 10:56:33

Advertisements