Python List remove[]
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]How to remove an element from a list in Python
Python provides the following methods to remove one or multiple elements. We can delete the elements using the del keyword by specifying the index position. Let's understand the following methods.
- remove[]
- pop[]
- clear[]
- del
- List Comprehension - If specified condition is matched.
The remove[] method
The remove[] method is used to remove the specified value from the list. It accepts the item value as an argument. Let's understand the following example.
Example -
Output:
If the list contains more than one item of the same name, it removes that item's first occurrence.
Example -
Output:
The pop[] method
The pop[] method removes the item at the specified index position. If we have not specified the index position, then it removes the last item from the list. Let's understand the following example.
Example -
Output:
We can also specify the negative index position. The index -1 represents the last item of the list.
Example -
Output:
The clear[] method
The clear[] method removes all items from the list. It returns the empty list. Let's understand the following example.
Example -
Output:
The del statement
We can remove the list item using the del keyword. It deletes the specified index item. Let's understand the following example.
Example -
Output:
It can delete the entire list.
Output:
We can also delete the multiple items from the list using the del with the slice operator. Let's understand the following example.
Example -
Output:
Using List Comprehension
The list comprehension is slightly different way to remove the item from the list. It removes those items which satisfy the given condition. For example - To remove the even number from the given list, we define the condition as i % 2 which will give the reminder 2 and it will remove those items that's reminder is 2.
Let's understand the following example.
Example -
Output:
Remove multiple elements from a list in Python
Given a list of numbers, write a Python program to remove multiple elements from a list based on the given condition.
Example:
Input: [12, 15, 3, 10] Output: Remove = [12, 3], New_List = [15, 10] Input: [11, 5, 17, 18, 23, 50] Output: Remove = [1:5], New_list = [11, 50]Multiple elements can be deleted from a list in Python, based on the knowledge we have about the data. Like, we just know the values to be deleted or also know the indexes of those values. Let’s see different examples based on different scenario.
Example #1: Let’s say we want to delete each element in the list which is divisible by 2 or all the even numbers.
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# Iterate each element in list
# and add them in variable total
for ele in list1:
if ele % 2 == 0:
list1.remove[ele]
# printing modified list
print["New list after removing all even numbers: ", list1]
|
Output:
New list after removing all even numbers: [11, 5, 17, 23]
Example #2: Using list comprehension
Removing all even elements in a list is as good as only including all the elements which are not even[ i.e. odd elements].
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# will create a new list,
# excluding all even numbers
list1 = [ elem for elem in list1 if elem % 2 != 0]
print[*list1]
|
Output:
11 5 17 23Example #3: Remove adjacent elements using list slicing
Below Python code remove values from index 1 to 4.
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# removes elements from index 1 to 4
# i.e. 5, 17, 18, 23 will be deleted
del list1[1:5]
print[*list1]
|
Output:
11 50Example #4: Using list comprehension
Let’s say the elements to be deleted is known, instead of the indexes of those elements. In this case, we can directly eliminate those elements without caring about indexes which we will see in next example.
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# items to be removed
unwanted_num = {11, 5}
list1 = [ele for ele in list1 if ele not in unwanted_num]
# printing modified list
print["New list after removing unwanted numbers: ", list1]
|
Output:
New list after removing unwanted numbers: [17, 18, 23, 50]Example #5: When index of elements is known.
Though indexes of elements in known, deleting the elements randomly will change the values of indexes. Hence, it is always recommended to delete the largest indices first. Using this strategy, index of smaller values will not be changed. We can sort the list in reverse order and delete the elements of list in descending order.
# Python program to remove multiple
# elements from a list
# creating a list
list1 = [11, 5, 17, 18, 23, 50]
# given index of elements
# removes 11, 18, 23
unwanted = [0, 3, 4]
for ele in sorted[unwanted, reverse = True]:
del list1[ele]
# printing modified list
print [*list1]
|
Output:
5 17 50Python | Remove given element from the list
Given a list, write a Python program to remove the given element [list may have duplicates] from given list. There are multiple ways we can do this task in Python. Let’s see some of Pythonic ways to do this task.
Method #1: Using pop[] method [Remove given element found first.]
# Python program to remove given element from the list
list1 = [1, 9, 8, 4, 9, 2, 9]
# Printing initial list
print ["original list : "+ str[list1]]
remove = 9
# using pop[]
# to remove list element 9
if remove in list1:
list1.pop[list1.index[remove]]
# Printing list after removal
print ["List after element removal is : " + str[list1]]
|
Method #2: Using remove[] method
# Python program to remove given element from the list
list1 = [1, 9, 8, 4, 9, 2, 9]
# Printing initial list
print ["original list : "+ str[list1]]
# using remove[] to remove list element 9
list1.remove[9]
# Printing list after removal
print ["List after element removal is : " + str[list1]]
|
original list : [1, 9, 8, 4, 9, 2, 9] List after element removal is : [1, 8, 4, 9, 2, 9]
Now, let’s see the ways to remove all occurrence of given element.
Method #3: Using set
Since the list is converted to set, all duplicates are removed, but the ordering of list cannot be preserved.
# Python program to remove given element from the list
list1 = [1, 9, 8, 4, 9, 2, 9]
# Printing initial list
print ["original list : "+ str[list1]]
# using discard[] method to remove list element 9
list1 = set[list1]
list1.discard[9]
list1 = list[list1]
# Printing list after removal
print ["List after element removal is : " + str[list1]]
|
Method #4: Using list comprehension
# Python program to remove given element from the list
list1 = [1, 9, 8, 4, 9, 2, 9]
# Printing initial list
print ["original list : "+ str[list1]]
# using List Comprehension
# to remove list element 9
list1 = [ele for ele in list1 if ele != 9]
# Printing list after removal
print ["List after element removal is : " + str[list1]]
|
Python remove[] method
Python removes [] method is a built-in method available with the list. It helps to remove the given very first element matching from the list.
Syntax:
list.remove[element]The element that you want to remove from the list.
ReturnValue
There is no return value for this method.
Tips for using remove[] method:
Following are the important points to remember when using remove [] method:
- When the list has duplicate elements, the very first element that matches the given element will be removed from the list.
- If the given element is not present in the list, it will throw an error saying the element is not in the list.
- The remove [] method does not return any value.
- The remove [] takes the value as an argument, so the value has to pass with the correct datatype.
Example: Using remove[] method to remove an element from the list
Here is a sample list that i have
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya']The list has elements of date-types string and number. The list has duplicate elements like number 12 and string Riya.
my_list = [12, 'Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] my_list.remove[12] # it will remove the element 12 at the start. print[my_list] my_list.remove['Riya'] # will remove the first Riya from the list print[my_list] my_list.remove[100] #will throw an error print[my_list]Output:
['Siya', 'Tiya', 14, 'Riya', 12, 'Riya'] ['Siya', 'Tiya', 14, 12, 'Riya'] Traceback [most recent calllast]: File "display.py", line 9, in my_list.remove[100] ValueError: list.remove[x]: x not in the listPython - Remove List Items
Remove Specified Item
The remove[] method removes the specified item.
Example
Remove "banana":
thislist.remove["banana"]
print[thislist]
Remove an item from a list in Python [clear, pop, remove, del]
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.
- Remove all items: clear[]
- Remove an item by index and get its value: pop[]
- Remove an item by value: remove[]
- Remove items by index or slice: del
- Remove items that meet the condition: List comprehensions
See the following article for adding items to the list.
- Add an item to a list in Python [append, extend, insert]