You sometimes encounter situations while programming where you have to remove element from list python if it is incorrect or no longer needed, but you do not know its index value. To overcome this, Python allows you to remove items from a list based on their value with the remove() method.
remove() Parameters
We follow the following syntax for the remove() method:
list.remove(element)
The function only accepts a single parameter (element) that it removes from the list. If the element is absent from the list, it raises a ValueError: list.remove(x): x not in list exception.
Return Value from remove()
Unlike the pop() method, remove() does not return any value (None) and only deletes the element.
Example of Removing element from the list
Let us understand how we can use remove() through an example:
# create list
fruits = ['apple', 'grapes', 'banana', 'guava']
# remove grapes
fruits.remove('grapes')
print ('Current list:', fruits)
Output:
Current list: ['apple', 'banana', 'guava']
Example of remove() method on a list that has duplicate elements
Lists are ordered and changeable sequences that allow duplicate elements to exist within them. When we use the remove() method to remove an element from a list in Python, it removes only the first element that matches the specified parameter.
# fruits list
fruits = ['apple', 'grapes', 'grapes', 'banana', 'guava', 'grapes', 'banana']
fruits.remove('grapes')
print ('Current list:', fruits)
Output:
Current list: ['apple', 'grapes', 'banana', 'guava', 'grapes', 'banana']
In the above example, only the first ‘grapes’ element that occurs gets removed.
Example of deleting an element that doesn’t exist
# fruits list
fruits = ['apple', 'grapes', 'grapes', 'banana', 'guava', 'grapes', 'banana']
fruits.remove('pineapple') # 'pineapple' does not exist in the list
print ('Current list:', fruits)
Output:
Traceback (most recent call last):
File "", line 3, in
ValueError: list.remove(x): x not in list
Questions and Answers
Q1. How do I remove a specific element from a list in Python?
You can use the remove() method to remove an element from a list in Python. To remove an element, you have to pass a single parameter by its value. You can use this function as:
# name list
name = ['Bob', 'Phil', 'Alice', 'Noah']
name.remove('Phil')
print ('New list:', name)
Output:
New list: ['Bob', 'Alice', 'Noah']
Q2. How do you remove multiple elements from a list in Python?
We use the del keyword with an index range to remove multiple items from a list.
For example:
# fruits list
fruits = [‘apple’, ‘grapes’, ‘grapes’, ‘banana’, ‘guava’, ‘grapes’, ‘banana’]
del fruits [1:4]
print (‘Current list:’, fruits)
Current list: [‘apple’, ‘guava’, ‘grapes’, ‘banana’]
Here, we set an index range from index position 1 to index position 4. In the output, you can see that elements in positions 1, 2, and 3 are deleted (position 4 does not get included in the range).
Q3. How do I remove all the elements from a list?
You can remove all the elements in a list by using the clear() method.
# fruits list
fruits = ['apple', 'grapes', 'grapes', 'banana', 'guava', 'grapes', 'banana']
fruits.clear() # does not accept any parameters
print ('Current list:', fruits)
Output:
Current list: []
Q4. How do you remove an item with an index from a list in Python?
You can either use the del keyword or the pop() function to remove an element from a list in Python by its index.
For example:
# name list
name = ['Bob', 'Phil', 'Alice', 'Noah', 'Jill', 'Jack', 'David']
# removing through pop()
name.pop(2) # removes 'Alice'
print ('New list:', name)
# removing through del
del name[5] # removes 'David'
print ('New list:', name)
Output:
New list: ['Bob', 'Phil', 'Noah', 'Jill', 'Jack', 'David']
New list: ['Bob', 'Phil', 'Noah', 'Jill', 'Jack']
Leave a Reply