EN
Python - remove list items
0 points
In this article, we would like to show you how to remove items from the list in Python.
Quick solution:
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
3
# remove by index
4
my_list.pop(1)
5
6
print(my_list) # ['A', 'C']
or
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
3
#remove by name
4
my_list.remove('B')
5
6
print(my_list) # ['A', 'C']
In this example, we use pop()
method to remove item with specified index.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
my_list.pop(1)
3
4
print(my_list) # ['A', 'C']
Output:
xxxxxxxxxx
1
['A', 'C']
In this example, we use remove()
method to remove item with specific value.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
my_list.remove('B')
3
4
print(my_list) # ['A', 'C']
Output:
xxxxxxxxxx
1
['A', 'C']
In this example, we use clear()
method to remove all items from the list.
xxxxxxxxxx
1
my_list = ['A', 'B', 'C']
2
my_list.clear()
3
4
print(my_list) # []
Output:
xxxxxxxxxx
1
[]