Removing an Element from a Python List

To remove an element from the list in Python, you can use the list.remove() or list.pop() methods, or the list.clear() method to remove all elements from the list. The list.remove() method removes the matching element passed as an argument. The list.pop(index) method removes the element at the given index from the list and returns the removed element. The list.pop() method removes the last element if no index is passed. You can also delete items using the "del" operator, specifying a position or range using an index or slice. In this Python List Remove example, we remove the specified list item using the list.remove() method. Click Execute to run the Python List Remove Example online and see the result.
Removing an Element from a Python List Execute
my_list = ["orange", "grape", "mango"]

my_list.remove("grape")

print(my_list)
Updated: Viewed: 6096 times

What is the List in Python?

Python lists are unordered collections of objects that allow you to organize and store data. Lists can contain items of many different types, such as strings, numbers, classes, and even other lists. Python lists are dynamic structures; list manipulation methods allow you to add, remove, or sort lists "in place", which can change the length of the list. To create a list and initialize it, you can enclose the values in square brackets and separate them with commas.

Python List Example
my_list = ["banana", "orange", "strawberry"]
      
print(my_list)

# ['banana', 'orange', 'strawberry']

How to remove an item by value from Python list?

To remove a specific item from a Python list, you can use the list.remove() method. If more than one element in the list matches the specified value, only the first occurrence of that element will be removed. Specifying a value that does not exist will cause a ValueError exception. The following is an example of removing an element by value from a Python list:

Python List Remove by Value Example
my_list = ["orange", "grape", "mango"]

my_list.remove("grape")
      
print(my_list)

# ['orange', 'mango']

How to remove an element by index in Python list?

To remove an item by index from a Python list, you can use the list.pop() method. The method returns the element with the given index and removes it from the list. If you don't specify an index for list.pop(), the method will remove the last item. If the index is out of range, an IndexError exception will be thrown. The following is an example of deleting an element by index from a Python list:

Python List Remove by Index Example
my_list = ["orange", "grape", "mango"]

my_list.pop()

print(my_list)

# ['orange', 'grape']

How to remove all elements from Python list?

To remove all elements for a Python list, you can use the list.clear() method, which takes no parameters. The method does not return any value. The following is an example of removing all elements from a Python list:

Python List Remove All Elements Example
my_list = ["orange", "grape", "mango"]

my_list.clear()

print(my_list)

# []

How to remove list items using the del operator in Python?

To remove items from a list using the del statement in Python, you need to specify the position or range of the item(s) to be removed by index. The first index is 0, and the last index is -1. You can also delete the entire list by specifying [::-1] as the range. The following is an example of deleting a list element using the del statement in Python:

Delete the entire list with the del statement
my_list = ["orange", "grape", "mango", "cherry", "apple"]

del my_list[1:3]

print(my_list)

# ['orange', 'cherry', 'apple']

See also