Remove element from a list using pop() method

The pop() is also a method of listing. We can remove the element at the specified index and get the value of that element using pop(). Here, we first find the index position of 9, and then we will pass the index to pop() function.

Python3




# 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:
  # get index of 9 and pop it out
    list1.pop(list1.index(remove))
     
# Printing list after removal
print ("List after element removal is : " + str(list1))


Output:

original list : [1, 9, 8, 4, 9, 2, 9]
List after element removal is : [1, 8, 4, 9, 2, 9]

Time Complexity: O(n)
Auxiliary Space: O(n)

Python | Remove given element from the list

Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let’s see some of the Pythonic ways to do this task.

Example:

Input:  [1, 8, 4, 9, 2]
Output: [1, 8, 4, 2]
Explanation: The Element 9 is Removed from the List.

Similar Reads

Remove element from a list using pop() method

The pop() is also a method of listing. We can remove the element at the specified index and get the value of that element using pop(). Here, we first find the index position of 9, and then we will pass the index to pop() function....

Delete an element from a list using remove() method

...

Remove an element from a list using list comprehension

We can remove an item from the list by passing the value of the item to be deleted as the parameter to remove() function....

Delete an item from a list using the del

...

Remove the given element from a list using a set

In this method, we are using list comprehension. Here, we are appending all the elements except the elements that have to be removed....

Using a combination of list comprehension and filter with help of Lambda Function:

...

Using Recursion Method

The Python del statement is not a function of List. Items of the list can be deleted using the del statement by specifying the index of the item (element) to be deleted....

Using enumeration

...