
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
How to remove an element from a list by index in Python?
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.
- Related Articles
- How to remove an element from a list in Python?
- How to remove an item from a C# list by using an index?
- How to remove index list from another list in python?
- How to remove an element from a Java List?
- How to remove an array element by its index in MongoDB?
- How to remove element from the specified index of the List in C#?
- How to remove an element from Array List in C#?
- How to remove an object from a list in Python?
- How do you remove an array element by its index in MongoDB
- Python prorgam to remove duplicate elements index from other list
- Python program to compute the power by Index element in List
- How to find what is the index of an element in a list in Python?
- Java Program to remove an element from List with ListIterator
- How can I remove the same element in the list by Python
- Python – Remove Tuples from a List having every element as None
