How to remove index list from another list in python?


In this article, we will show you how to remove the index list elements from the original list using python. Now we see 2 methods to accomplish this task −

  • Using pop() method

  • Using del keyword

Assume we have taken a list containing some elements. We will remove the index list elements from the main list using different methods as specified above.

Note

We must sort the indices list in descending order because removing elements from the beginning will change the indices of other elements, and removing another element will result in an incorrect result due to misplaced indices.

Let us use an example to demonstrate this.

Consider the following List −

list = ["Welcome," "to," "tutorialspoint," "python"]
indices List = [ 0, 2]

Problem with Removing the elements from starting

If we remove elements from the beginning, the element at 0 index “Welcome”, will be removed first.

Now the modified list looks like this −

list = ["to," "tutorialspoint," "python"]

If we remove the element at 2 index i.e "python", the modified list looks like this

list = ["to," "tutorialspoint]

But the result should be

list = ["to”,"python"]

Because the elements at indices 0 and 2 are “Welcome” and “tutorialspoint”.

So to overcome this we remove elements from the end such that indices will not get affected.

Method 1: Using pop() function

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

The following program returns the list after deleting the elements of the main/original list based on the index list using pop() function −

# input list inputList = ["Welcome", 20, "to", "tutorialspoint", 30, "python"] # Entering the indices list at which the items are to be deleted givenIndices = [1, 4, 5] # 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

On executing, the above program will generate the following output −

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

We created a list and added some random values to it. We then created another index list to store the indices of the main list elements that were to be removed. To avoid conflicts while removing, we sorted the indices list in descending/decreasing order. After that, we traversed the index list, checking whether the index is less than the length of the main list for each element, as we can only remove elements with indexes less than the length of the list. Then, by passing index, we removed that element from the main list and displayed the final main list after removing indices list index elements from the main list.

Method 2: Using del() function

Algorithm (Steps)

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

  • Using the sorted() function to sort the given indices list in descending order by passing given indices list and reverse=True as arguments to it.

  • 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)

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

  • Print the list after deleting all the elements of the index list.

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 elements of the main/original list based on the index list using the del keyword −

inputList = ["Welcome", 20, "to", "tutorialspoint", 30, "python"] # index list givenIndices = [0, 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 del keyword del inputList[indx] # printing the list after deleting items at the given indices print("List after deleting items at the given indices:\n") print(inputList)

Output

List after deleting items at the given indices:
[20, 'to', 30, 'python']

Conclusion

In this article, we learned how to remove an index list from an original list using the pop() function and the del keyword. Using a practical example, we also learned why we can't remove the elements from the start.

Updated on: 19-Sep-2022

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements