Table of Contents
In this tutorial, we will learn about the Python List remove() method with the help of examples.
The remove() method removes the first matching element (which is passed as an argument) from the list.
Example
# create a list
prime_numbers = [2, 3, 5, 7, 9, 11]
# remove 9 from the list
prime_numbers.remove(9)
# Updated prime_numbers List
print('Updated List: ', prime_numbers)
# Output: Updated List: [2, 3, 5, 7, 11]
1. Syntax of List remove()
The syntax of the remove() method is:
list.remove(element)
2. remove() Parameters
- The
remove()method takes a single element as an argument and removes it from the list. - If the
elementdoesn’t exist, it throws ValueError: list.remove(x): x not in list exception.
3. Return Value from remove()
The remove() doesn’t return any value (returns None).
4. Example 1: Remove element from the list
# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']
# 'rabbit' is removed
animals.remove('rabbit')
# Updated animals List
print('Updated animals list: ', animals)
Output
Updated animals list: ['cat', 'dog', 'guinea pig']
5. Example 2: remove() method on a list having duplicate elements
If a list contains duplicate elements, the remove() method only removes the first matching element.
# animals list
animals = ['cat', 'dog', 'dog', 'guinea pig', 'dog']
# 'dog' is removed
animals.remove('dog')
# Updated animals list
print('Updated animals list: ', animals)
Output
Updated animals list: ['cat', 'dog', 'guinea pig', 'dog']
Here, only the first occurrence of element ‘dog’ is removed from the list.
6. Example 3: Deleting element that doesn’t exist
# animals list
animals = ['cat', 'dog', 'rabbit', 'guinea pig']
# Deleting 'fish' element
animals.remove('fish')
# Updated animals List
print('Updated animals list: ', animals)
Output
Traceback (most recent call last):
File ".. .. ..", line 5, in <module>
animal.remove('fish')
ValueError: list.remove(x): x not in list
Here, we are getting an error because the animals list doesn’t contain 'fish'.
- If you need to delete elements based on the index (like the fourth element), you can use the pop() method.
- Also, you can use the Python del statement to remove items from the list.
Related posts:
Python Program to Delete an Element From a Dictionary
Python Program to Convert Two Lists Into a Dictionary
Python exec()
Python Tuple count()
Python @property decorator
Python Program to Differentiate Between del, remove, and pop on a List
Python object()
Python slice()
Python min()
Python Program to Get a Substring of a String
Python hex()
Python Program Read a File Line by Line Into a List
Python getattr()
Python timestamp to datetime and vice-versa
Python Objects and Classes
Python tuple()
Python Program to Print all Prime Numbers in an Interval
Using a List of Values in a JdbcTemplate IN Clause
Python Program to Iterate Through Two Lists in Parallel
Python String isnumeric()
Python Set clear()
Python bytes()
Python Program to Find the Size (Resolution) of a Image
Python Program to Find All File with .txt Extension Present Inside a Directory
Python Program to Access Index of a List Using for Loop
Python Program to Find the Square Root
Remove the First Element from a List
Deep Learning with Python - Francois Cholletf
Finding Max/Min of a List or Collection
Python Program to Convert Bytes to a String
Python Program to Check if a Number is Odd or Even
Python Input, Output and Import