In Python, use list methods clear(), pop(), and remove() to remove items (elements) from a list. It is also possible to delete items using del statement by specifying a position or range with an index or slice.,clear(), pop(), and remove() are methods of list. You can also remove elements from a list with del statements.,To remove multiple items that satisfy the condition at once, use the list comprehensions described below.,Remove items that meet the condition: List comprehensions
l = [0, 1, 2] l.clear() print(l) #[]
l = [0, 10, 20, 30, 40, 50] print(l.pop(0)) # 0 print(l) #[10, 20, 30, 40, 50] print(l.pop(3)) # 40 print(l) #[10, 20, 30, 50]
print(l.pop(-2))
# 30
print(l)
#[10, 20, 50]
print(l.pop())
# 50
print(l)
#[10, 20]
# print(l.pop(100)) # IndexError: pop index out of range
l = ['Alice', 'Bob', 'Charlie', 'Bob', 'Dave']
l.remove('Alice')
print(l)
#['Bob', 'Charlie', 'Bob', 'Dave']
By the end, you'll know how to use remove() to remove an item from a list in Python.,And there you have it! You now know how to remove a list item in Python using the remove() method. You also saw some ways of removing all occurrences of an item in a list in Python.,The remove() method is one of the ways you can remove elements from a list in Python.,remove() removes only the first occurrence of an item How to remove all occurrences of an item
The general syntax of the remove()
method looks like this:
list_name.remove(value)
remove()
will search the list to find it and remove it.
#original list programming_languages = ["JavaScript", "Python", "Java", "C++"] #print original list print(programming_languages) # remove the value 'JavaScript' from the list programming_languages.remove("JavaScript") #print updated list print(programming_languages) #output #['JavaScript', 'Python', 'Java', 'C++'] #['Python', 'Java', 'C++']
If you specify a value that is not contained in the list, then you'll get an error – specifically the error will be a ValueError
:
programming_languages = ["JavaScript", "Python", "Java", "C++"]
#I want to remove the value 'React'
programming_languages.remove("React")
#print list
print(programming_languages)
#output
# line 5, in <module>
#programming_languages.remove("React")
#ValueError: list.remove(x): x not in list
Another way to avoid this error is to create a condition that essentially says, "If this value is part of the list then delete it. If it doesn't exist, then show a message that says it is not contained in the list".
programming_languages = ["JavaScript", "Python", "Java", "C++"]
if "React" in programming_languages:
programming_languages.remove("React")
else:
print("This value does not exist")
#output
#This value does not exist
Let's look at the following example:
programming_languages = ["JavaScript", "Python", "Java", "Python", "C++", "Python"]
programming_languages.remove("Python")
print(programming_languages)
#output
#['JavaScript', 'Java', 'Python', 'C++', 'Python']
EXAMPLE:
myList = ["Bran", 11, 3.14, 33, "Stark", 22, 33, 11]
print(myList)
myList = ["Bran", 11, 22, 33, "Stark", 22, 33, 11]
myList.remove(22)
myList
EXAMPLE 2:
myList.remove(44)
EXAMPLE 2:
myList
EXAMPLE 3:
myList.pop(8)
Remote Work 2022 , Remote Work 2022
list.remove(value)
Code to remove item from list by value:
list1 = ["Hire", "Hire", "the", "top", 10, "python", "freelancers"]
list1.remove("Hire")
print(list1)
#Output - ['Hire', 'the', 'top', 10, 'python', 'freelancers']
Syntax of pop()
list.pop(index)
Syntax of del:
del object_name
Code to remove items using del
list1 = ["Hire", "the", "top", 10, "python", "freelancers"]
del list1[0]
print(list1)
#Output - "['the', 'top', 10, 'python', 'freelancers']"
The remove() method takes a single element as an argument and removes it from the list.,The remove() method removes the first matching element (which is passed as an argument) from the list.,If a list contains duplicate elements, the remove() method only removes the first matching element.,Also, you can use the Python del statement to remove items 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]
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]
The syntax of the remove()
method is:
list.remove(element)
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)
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)
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)