How to remove an element from a Python list?
To remove an element from a Python list, you can use the following method:
- delete
my_list = [1, 2, 3, 4, 5]
my_list.remove(3)
print(my_list) # Output: [1, 2, 4, 5]
- delete
my_list = [1, 2, 3, 4, 5]
del my_list[2]
print(my_list) # Output: [1, 2, 4, 5]
- remove the last added element
my_list = [1, 2, 3, 4, 5]
removed_element = my_list.pop(2)
print(my_list) # Output: [1, 2, 4, 5]
print(removed_element) # Output: 3