How to Delete Element from List in Python
Learn how to delete elements from a list in Python with this comprehensive guide. Understand the concept, step-by-step explanation, code snippets, and more.| …
Updated July 2, 2023
|Learn how to delete elements from a list in Python with this comprehensive guide. Understand the concept, step-by-step explanation, code snippets, and more.|
How to Delete Element from List in Python
Definition of the Concept
In programming, a list is an ordered collection of values that can be of any data type, including strings, integers, floats, and other lists. Deleting an element from a list means removing one or more elements from its current position within the list.
Step-by-Step Explanation
To delete an element from a list in Python, you have several methods to choose from depending on your specific needs. Let’s explore each of them:
Method 1: Using the del
Statement
The del
statement is used to delete elements from a list by their index position.
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
del my_list[2] # Deletes the element at index 2 (i.e., 30)
print("List after deletion:", my_list)
Explanation: In this example, my_list
is a list containing integers from 10 to 50. The line del my_list[2]
deletes the element at index position 2, which in this case is 30. After deletion, the updated list is printed out.
Method 2: Using the remove()
Method
The remove()
method removes the first occurrence of a specified value from the list.
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
my_list.remove(30) # Removes the element with value 30
print("List after removal:", my_list)
Explanation: Here, remove()
is used to delete the first occurrence of the number 30 from the list. Note that if there are multiple occurrences of the same value in a list and you want to remove all of them, using a loop would be more appropriate than calling remove()
repeatedly.
Method 3: Using List Comprehensions
List comprehensions can also be used to filter elements out of a list by creating a new list that excludes certain values or conditions.
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
new_list = [x for x in my_list if x != 30] # Creates a new list excluding the element with value 30
print("List created using comprehension:", new_list)
Explanation: This approach creates a new list (new_list
) by iterating over my_list
but only including elements that are not equal to 30. The result is similar to removing 30 from my_list
, but note that it does not modify the original list.
Method 4: Using Filter Function
Python’s built-in function filter() can also be used along with a lambda function to remove items based on certain conditions.
my_list = [10, 20, 30, 40, 50]
print("Original List:", my_list)
filtered_list = list(filter(lambda x: x != 30, my_list)) # Removes the element with value 30
print("List created using filter():", filtered_list)
Explanation: This approach uses a lambda function inside filter()
to create an iterator (filtered_list
) that only includes elements not equal to 30. Like list comprehensions, it does not modify the original list.
Conclusion
Deleting elements from a list in Python can be achieved through several methods depending on your specific needs or preferences. The choice among these methods (using del
, remove()
, list comprehensions, and the filter function) should align with how you interpret and approach the problem you’re trying to solve.