In Python, lists are utilized for storing numerous items of any data type in a single variable. It is basically referred to as the data type that stores a collection of objects. The lists in Python are declared using the square brackets. There are multiple methods and functions that are implemented on lists to achieve a specific functionality in Python. One such functionality is removing elements/items from a Python list.
In this write-up, we will demonstrate certain methods that help us remove the elements from the lists using the index value.
How to Remove/Delete Elements From a Python List by the Index?
The elements from the list can be removed by specifying the index value of the element that is to be removed. The index position of the element to be deleted is declared and the modified list after the deletion can be printed for verification. The remove() method is not the only method that deletes the element from the list. Some other well-known approaches can also be implemented for removing the element from the list using its index position. Those methods/approaches are listed below:
- Removing List Elements Using pop()
- Removing List Elements Using del
- Removing List Elements Using remove()
- Removing Multiple List Elements Using del
- Removing List Elements Using Enumerate and for Loop
- Removing List Elements Using Enumerate and List Comprehension
Below is the discussion of methods to remove the elements by declaring the index position in Python.
Method 1: Removing List Elements Using pop()
In Python, the pop() method takes in an integer/index value as an argument and deletes the corresponding element from the list. The code below illustrates the execution of the pop() method to delete the element in the list.
#Print the original List list_A = ["Programming", "in", "Java", "is", "fun"] print("Original List:",list_A) #Pass the index for the element to be removed list_A.pop(2) #Print the list after removing print("List after deleting:",list_A)
In the above code block:
- A list is declared as “list_A” in Python and is printed using the print function.
- The pop() function removes the element from index number 2 as declared.
- The modified list is printed.
Output
The output below shows that in the modified list, “Java” is missing since it was present at index number 2, and the pop() method succeeded in deleting it.
Method 2: Removing List Elements Using del
In Python, the del keyword helps us remove a particular object such as a list or a specific part of the list. The below snippet demonstrates the execution of the del keyword to remove the element from the list.
#Print the original List list_A = ["Programming", "in", "Java", "is", "fun"] print("Original List:",list_A) #Pass the index for the element to be removed del list_A[1] #Print the list after removing print("List after deleting:",list_A)
In the above Python code,
- The del keyword is used to delete the element using the index value.
- The original and the modified lists are printed accordingly using the print() function.
Output
The below output depicts that the element “in” is absent from index number 1.
Method 3: Removing List Elements Using remove()
The remove() method accepts a particular element as an argument and removes it from the selected list. It can also be utilized to delete a specific list element based on its index. Consider the following code for demonstration.
list_A= ["Welcome", "to", "Python", "Programming"] print("The Original List is :", list_A) list_A.remove(list_A[3]) print("Modified List after deleted item:\n", list_A)
Here in the above code,
- First, a list is created and printed on the console.
- Next, the remove() method is implemented to remove a list element from the index 3.
- Finally, the modified/updated list is printed using the print() function.
Output
The string “Programming” is removed since the index number 3 was specified in the remove() method.
Method 4: Removing Multiple List Elements Using the del Keyword
The del keyword can used within a loop to delete multiple list elements. In the code below, multiple indexes are declared and the del keyword is implemented to remove the element from the specified lists using the index.
# Declare the list list_A =["Programming", "in", "Java", "is", "fun"] print("Original List: ",list_A) # the indexes to be removed indxs=[0,1] # Remove multiple elements from the list for i in sorted(indxs, reverse=True): del list_A[i] print("Final List: ",list_A)
In the above code,
- The index to be removed are specified in a new list.
- The sorted() function is used within a for loop to iterate through the indexes to be deleted.
- The del keyword is utilized in the loop’s body to remove the list elements based on the specified indexes.
Output
In the below output, the elements “Programming” and “in” are removed from index positions 0 and 1 respectively.
Method 5: Removing List Elements Using Enumerate and for Loop
The enumerate in Python is a built-in function that allows one to iterate over the elements and a record of index numbers. It can be utilized to remove list elements based on their indexes. Below is the implementation of enumerate and for loop to remove the element from the List.
# Use enumerate() + loop str1 = ["Hello", "in","Python"] #The original list before deletion print("Original List: ",str1) rem=[1] n = [] for indx, element in enumerate(str1): # Checking for the presence of the element if indx not in rem: n.append(element) #Print the modified list print("Modified List: ",n)
In the code above:
- The List is declared as “str1” in Python.
- The index to be deleted is 1, which is declared in “rem”.
- A new empty list is created to store the elements of the modified list.
- In the next step for loop along with enumerate iterates over the elements present in the list.
- The append() function is used to add all the values into the specified empty list except the value that is to be removed.
Output
The output verifies the deletion of the list element from the specified index:
Method 6: Removing List Elements Using Enumerate and List Comprehension
In Python, List comprehension is used to make the syntax shorter/concise by making a new list from some other list or string. The below code describes the execution of enumerate and list comprehension to delete a specific list element using its index.
# Use enumerate() + loop str1 = ["Hello", "in", "Python"] #The original list before deletion print("Original List: ",str1) rem=[1,2] # Use enumerate() and List Comprehension n = [ele for idx, ele in enumerate(str1) if idx not in rem] #The modified list print("Modified List: ",n)
In the code above
- The list comprehension is implemented using the enumerate that matches the declared index value of the element to be removed.
- If the index is found it removes and returns the modified list.
- The modified list is printed to verify the deletion of the selected list element.
Output
The below output depicts that the elements in the list “in” and “Python” are removed by declaring the index values 1 and 2, respectively.
This concludes the discussion on different methods to remove or delete the element by declaring its index position in Python.
Conclusion
To remove an element by index in Python, use the pop() method, del keyword, enumerate with for loop, enumerate with list comprehension, or remove() method. Among them, the most convenient and commonly used methods include the “pop()” method, the del keyword, and the remove() method. This article has effectively demonstrated the implementation of different methods to discard the element by its index position in Python