This article shows how to remove Elements from List in Python.
Contents
- del : Remove elements by index or slice
- remove() : Remove an element by value
- pop() : Remove an element by index and get its value
- clear() : Remove all elements
sponsored link
del : Remove elements by index or slice
"del" statement can be used to delete elements from list by index or slice specification.
del List[ index ]
del List[ start index : end index ]
In the following sections, I show example code.
Remove elements by index
Specify element by index and remove from list.
### Remove elements by index
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango", "Lemon", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
del fruits[1]
print(fruits)
# ['Apple', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
del fruits[-1]
print(fruits)
# ['Apple', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach']
del fruits[3]
print(fruits)
# ['Apple', 'Orange', 'Grape', 'Lemon', 'Peach']
Remove elements by slice
Multiple elements can also be removed with "del" statement.
Specify elements by slice and remove from list.
### Remove elements by slice
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango", "Lemon", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
del fruits[2:4]
print(fruits)
# ['Apple', 'Banana', 'Mango', 'Lemon', 'Peach', 'Plum']
sponsored link
remove() : Remove an element by value
By using remove() method, the specified value is searched for and the first element is removed from list.
List.remove( value )
In the following sections, I show example code.
If there is only one element to be deleted.
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango", "Lemon", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
fruits.remove("Banana")
print(fruits)
# ['Apple', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
If there are multiple elements to be deleted.
fruits = ["Apple", "Banana", "Orange", "Apple", "Grape", "Mango", "Lemon", "Apple", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Apple', 'Grape', 'Mango', 'Lemon', 'Apple', 'Peach', 'Plum']
fruits.remove("Apple")
print(fruits)
# ['Banana', 'Orange', 'Apple', 'Grape', 'Mango', 'Lemon', 'Apple', 'Peach', 'Plum']
By using remove() method, only the first element is removed if there are multiple elements of the same type.
sponsored link
pop() : Remove an element by index and get its value
By using pop() method, the element at the specified position is deleted and its value is retrieved.
List.pop( index )
Here is an example code.
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango", "Lemon", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
value = fruits.pop(2)
print(value)
# Orange
print(fruits)
# ['Apple', 'Banana', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
value = fruits.pop()
print(value)
# Plum
print(fruits)
# ['Apple', 'Banana', 'Grape', 'Mango', 'Lemon', 'Peach']
If no index is specified, the last element of the list is deleted.
sponsored link
clear() : Remove all elements
By using clear() method, all elements are removed.
List.clear( )
Here is an example code.
fruits = ["Apple", "Banana", "Orange", "Grape", "Mango", "Lemon", "Peach", "Plum"]
print(fruits)
# ['Apple', 'Banana', 'Orange', 'Grape', 'Mango', 'Lemon', 'Peach', 'Plum']
fruits.clear()
print(fruits)
# []
sponsored link