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.