Remove an Element from a List by Index in Python
Last Updated :
27 Feb, 2024
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python.
Example:
Input: [1, 2, 3, 4, 5], index = 2
Output: [1, 2, 4, 5]
Explanation: Element present in 2nd index is removed i.e, 3
Remove an Element from a List by Index in Python
Below, are the methods of remove an element from a list by using the index value in Python.
Remove an Element from a List by Index Using List Comprehension
In this example, below code uses list comprehension to create a new list (`updated_list`) excluding the element at index 2 from the original list [1, 2, 3, 4, 5]. The output is [1, 2, 4, 5].
Python3
original_list = [1, 2, 3, 4, 5]
index_to_remove = 2
updated_list = [element for i, element in enumerate(original_list) if i != index_to_remove]
print(updated_list)
Remove an Element from a List by Index Using the del keyword
In this example, below code utilizes the `del` keyword to remove the element at index 2 from the `original_list` [1, 2, 3, 4, 5], resulting in the modified list [1, 2, 4, 5].
Python3
original_list = [1, 2, 3, 4, 5]
index_to_remove = 2
del original_list[index_to_remove]
print(original_list)
Remove an Element from a List by Index Using remove() Function
In this example, below code employs the `remove()` function to eliminate the element with the value 3 from the `original_list` [1, 2, 3, 4, 5], yielding the updated list [1, 2, 4, 5].
Python3
original_list = [1, 2, 3, 4, 5]
value_to_remove = 3
original_list.remove(value_to_remove)
print(original_list)
Remove an Element from a List by Index Using pop() Function
In this example, below In this code, the `pop()` function removes the element at index 2 from the `original_list` [1, 2, 3, 4, 5], modifying the list to [1, 2, 4, 5], and the removed element (3) is printed separately.
Python3
original_list = [1, 2, 3, 4, 5]
index_to_remove = 2
removed_element = original_list.pop(index_to_remove)
print(original_list)
print(f"Removed element: {removed_element}")
Output[1, 2, 4, 5]
Removed element: 3
Conclusion
In conclusion , Python provides several methods to remove elements from a list by index, each with its own advantages and use cases. Whether you prefer a concise list comprehension, the in-place modification with the del
keyword, or the specialized remove()
and pop()
functions, understanding these techniques will help you choose the most appropriate method for your specific programming needs.
Similar Reads
Remove Last Element from List in Python Given a list, the task is to remove the last element present in the list. For Example, given a input list [1, 2, 3, 4, 5] then output should be [1, 2, 3, 4]. Different methods to remove last element from a list in python are:Using pop() methodUsing Slicing TechniqueUsing del OperatorUsing Unpacking
2 min read
Remove first element from list in Python The task of removing the first element from a list in Python involves modifying the original list by either deleting, popping, or slicing the first element. Each method provides a different approach to achieving this. For example, given a list a = [1, 2, 3, 4], removing the first element results in
2 min read
Python - Print list after removing element at given index In this article, we will explore different ways to remove an element from a list at a given index and print the updated list, offering multiple approaches for achieving this.Using pop()pop() method removes the element at a specified index and returns it.Pythonli = [10, 20, 30, 40, 50] index = 2 li.p
2 min read
Remove all the occurrences of an element from a list in Python The task is to perform the operation of removing all the occurrences of a given item/element present in a list. Example Input1: 1 1 2 3 4 5 1 2 1 Output1: 2 3 4 5 2 Explanation : The input list is [1, 1, 2, 3, 4, 5, 1, 2] and the item to be removed is 1. After removing the item, the output list is [
4 min read
How to Remove Item from a List in Python Lists in Python have various built-in methods to remove items such as remove, pop, del and clear methods. Removing elements from a list can be done in various ways depending on whether we want to remove based on the value of the element or index.The simplest way to remove an element from a list by i
3 min read