Open In App

Python | Remove given element from the list

Last Updated : 24 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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.

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)

Delete an element from a list using remove() method 

We can remove an item from the list by passing the value of the item to be deleted as the parameter to remove() 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))
 
# using remove() to remove list element 9
list1.remove(9)
 
 
# 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)

Remove an element from a list using list comprehension

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

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))
 
# 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))


Output:

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

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

Delete an item from a list using the del 

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. 

Python3




lst = ['Iris', 'Orchids', 'Rose', 'Lavender',
    'Lily', 'Carnations']
print("Original List is :", lst)
 
# using del statement
# to delete item (Orchids at index 1)
# from the list
del lst[1]
print("After deleting the item :", lst)


Output: 

Original List is : [‘Iris’, ‘Orchids’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’] After deleting the item : [‘Iris’, ‘Rose’, ‘Lavender’, ‘Lily’, ‘Carnations’]

Time Complexity: O(n) where n is the elements in the list
Auxiliary Space: O(n), where n is the length of the list

Remove the given element from a list using a set 

Since the list is converted to set, all duplicates are removed, but the ordering of the list cannot be preserved. 

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))
 
# 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))


Output:

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

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

This solution uses filter to create a new list containing all the elements from list that are not equal to remove, and then assigns the resulting list to the result variable. This solution is similar to the solution using list comprehension and filter, but it is more concise and easier to read.

Python3




# Python program to remove given element from the list
 
lst = [1, 9, 8, 4, 9, 2, 9]
 
# Printing initial list
print("Original list:", lst)
 
remove = 9
 
# Using filter to remove the element
result = list(filter(lambda x: x != remove, lst))
 
# Printing list after removal
print("List after element removal:", result)
#This code is contributed by Edula Vinay Kumar Reddy


Output

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

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

Using Recursion Method

Here we use recursive method which returns the NewList where the does not contains the given element. we recursively call the function for n number of times to traverse all value in a list. 

Python3




# Python program to remove given element from the list
#defining recursive function to remove element
def remove_element(begin,oldlist,value,newlist):
  if begin==len(oldlist):  #base condition
    return newlist
  if value !=oldlist[begin]:  #check is element is not specified value
    newlist.append(oldlist[begin])
  return remove_element(begin+1,oldlist,value,newlist)  #recursive call
 
lst = [1, 9, 8, 4, 9, 2, 9]
value=9
# Printing initial list
print("Original list:", lst)
 
 
# Using recursive approach to remove the element
result = remove_element(0,lst,value,[])
 
 
# Printing list after removal
print("List after element removal:", result)
#This code is contributed by tvsk


Output

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

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

Using enumeration

  1. Create a list list1 containing some elements.
  2. Print the initial list.
  3. Use a for loop and enumerate() function to iterate through the list and get the index and elements at that index.
  4. Check if the element at the current index is equal to the element to be removed.
  5. If it is equal, remove that element using pop() function and the current index.
  6. Print the updated list.

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))
 
# using enumerate() to remove element 9
for i, ele in enumerate(list1):
    if ele == 9:
        list1.pop(i)
 
# Printing list after removal
print("List after element removal is : " + str(list1))
#This code is contributed by Vinay pinjala.


Output

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

The time complexity of the given code is O(n^2), where n is the length of the input list.

In the given code, we are iterating over each element of the input list using a for loop with enumerate(). Inside the loop, we are checking if the current element is equal to the value to be removed, and if it is, we are removing it using the pop() method. The pop() method has a time complexity of O(n), as it needs to shift all the subsequent elements by one index to fill the gap left by the removed element. Therefore, if we have multiple occurrences of the value to be removed, the time complexity can be much higher.

The auxiliary space of the given code is O(1), as we are not using any additional data structures to store the elements of the list. We are only modifying the original list in place.



Previous Article
Next Article

Similar Reads

Python | Remove given element from list of lists
The deletion of elementary elements from list has been dealt with many times, but sometimes rather than having just a one list, we have list of list where we need to perform this particular task. Having shorthands to perform this particular task can help. Let's discuss certain ways to perform this particular task. Method #1: Using list comprehensio
6 min read
Python | Remove element from given list containing specific digits
Given a list, the task is to remove all those elements from list which contains the specific digits. Examples: Input: lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 14, 13, 15, 16] no_delete = ['2', '3', '4', '0'] Output: [1, 5, 6, 7, 8, 9, 11, 15, 16] Explanation: Numbers 2, 3, 4, 10, 12, 13, 14 contains digits from no_delete, therefore remove them
9 min read
Ways to remove particular List element in Python
List is an important container and is used almost in every code of day-day programming as well as web development. The more it is used, more is the required to master it and hence knowledge of its operations is necessary. Let's see the different ways of removing particular list elements. Method #1 : Using remove() remove() can perform the task of r
6 min read
Python - Remove first element of list
The queue data structure is a very well-known data structure, lists in Python usually append the elements to the end of the Python list. For implementing a queue data structure, it is essential to be able to remove the front element from a list. Let's discuss the ways of removing the first element of the list. Method 1: Remove Elements From Lists i
5 min read
Python | Remove random element from list
Sometimes, while working with Python lists, we can have a problem or part of it, in which we desire to convert a list after deletion of some random element. This can have it's application in gaming domain or personal projects. Let's discuss certain way in which this task can be done. Method : Using randrange() + pop() In this, we just combine the f
3 min read
Python | Remove duplicates based on Kth element tuple list
Sometimes, while working with records, we may have a problem in which we need to remove duplicates based on Kth element of a tuple in the list. This problem has application in domains that uses records as input. Let's discuss certain ways in which this problem can be solved. Method #1: Using loop This is a brute-force method to perform this particu
8 min read
Python - Remove rear element from list
A stack data structure is a very well-known data structure, lists in Python usually append the elements to the end of the list. For implementing a stack data structure, it is essential to be able to remove the end element from a list. Let’s discuss the ways to achieve this so that stack data structure can be implemented easily using lists. Method #
6 min read
Python - Remove Tuples from the List having every element as None
Given a Tuple list, remove all tuples with all None values. Input : test_list = [(None, 2), (None, None), (3, 4), (12, 3), (None, )] Output : [(None, 2), (3, 4), (12, 3)] Explanation : All None tuples are removed.Input : test_list = [(None, None), (None, None), (3, 4), (12, 3), (None, )] Output : [(3, 4), (12, 3)] Explanation : All None tuples are
6 min read
Python Program to remove a specific digit from every element of the list
Given a list of elements, the task here is to write a Python program that can remove the presence of all a specific digit from every element and then return the resultant list. Examples: Input : test_list = [333, 893, 1948, 34, 2346], K = 3 Output : ['', 89, 1948, 4, 246] Explanation : All occurrences of 3 are removed. Input : test_list = [345, 893
7 min read
Python program to remove row with custom list element
Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result. Examples: Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15] Output : [[7, 8, 9], [12, 18, 21]] Explanation : [5, 3, 1] row has 3 in it in custom l
6 min read
Practice Tags :
three90RightbarBannerImg