Python | Remove given element from list of lists
Last Updated :
05 May, 2023
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 comprehension The logic behind this kind of method is to reduce the size of code and do the task to perform using loops as a way of list comprehension itself.
Python3
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
print ("The original list : " + str (test_list))
N = 4
res = [[ele for ele in sub if ele ! = N] for sub in test_list]
print ("The list after deletion of element : " + str (res))
|
Output :
The original list : [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The list after deletion of element : [[5, 6], [5, 6, 1], [], [8, 9, 10]]
Time Complexity: O(n), where n is the number of elements in the list test_list. The list comprehension is used to perform the task and it takes O(n) time.
Auxiliary Space: O(n), additional space of size n is created where n is the number of elements in the new res list.
Method 2: Using list comprehension + list slicing In this method, we generally do the task similar to the above method, the variation is just we use list slicing for better code readability.
Python3
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
print ("The original list : " + str (test_list))
N = 4
for sub in test_list:
sub[:] = [ele for ele in sub if ele ! = N]
print ("The list after deletion of element : " + str (test_list))
|
Output :
The original list : [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The list after deletion of element : [[5, 6], [5, 6, 1], [], [8, 9, 10]]
Time complexity: O(n*m), where n is the number of sublists in the list and m is the average length of the sublists.
Auxiliary space: O(1), since the list is modified in place and no additional data structure is used.
Method 3: Using enumerate function
Python3
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
N = 4
res = [[ele for j,ele in enumerate (sub) if ele ! = N] for i,sub in enumerate (test_list)]
print (res)
|
Output
[[5, 6], [5, 6, 1], [], [8, 9, 10]]
Method 4: Using remove() function
This approach involves looping through each sublist and removing the element using the remove() function. It can be implemented as follows:
Python3
def remove_element(lst, element):
for sub in lst:
sub.remove(element)
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
print ( "The original list :" , test_list)
remove_element(test_list, 4 )
print ( "The list after deletion of element :" , test_list)
|
Output
The original list : [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The list after deletion of element : [[5, 6], [5, 6, 1], [], [8, 9, 10]]
The time complexity of the remove_element() function using a for loop is O(n), where n is the total number of elements in the list of lists. This is because the function processes each element in the lists once.
The auxiliary space is O(1), because the function does not create any additional data structures to store the result. It modifies the original list in place.
Method 5: Using recursive function
Using recursive function method, we can remove the element in every nested list
Python3
def remove_element(start,oldlist,newlist,element):
if start = = len (oldlist): return newlist
sublist = []
for i in oldlist[start]:
if i = = element: pass
else :sublist.append(i)
newlist.append(sublist)
return remove_element(start + 1 ,oldlist,newlist,element)
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
print ( 'The Original list: ' ,test_list)
element = 4
print ( 'The List after deleted 4 in every nested list:' ,remove_element( 0 ,test_list,[],element))
|
Output
The Original list: [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The List after deleted 4 in every nested list: [[5, 6], [5, 6, 1], [], [8, 9, 10]]
Method 6: Using filter()
Python3
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
N = 4
print ( 'The Original list: ' ,test_list)
res = [ list ( filter ( lambda x: x ! = N, sub)) for sub in test_list]
print ( "The list after deletion of element : " + str (res))
|
Output
The Original list: [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The list after deletion of element : [[5, 6], [5, 6, 1], [], [8, 9, 10]]
Time Complexity: O(N*M)
Auxiliary Space: O(N*M)
Method 5: Using map() and filter() functions
This approach involves using the map() and filter() functions in combination with lambda functions to remove the specified element from the list of lists.
Algorithm:
- Initialize a variable “N” to the element that needs to be removed.
- Define a lambda function “remove_element” that returns a filtered sublist by removing the element “N” from the input sublist.
- Use the map() function to apply the “remove_element” function to each sublist in the input list and convert the resulting map object to a list.
Python3
def remove_element(N, sub): return list ( filter ((N).__ne__, sub))
test_list = [[ 4 , 5 , 6 ], [ 5 , 6 , 4 , 1 ], [ 4 ], [ 4 , 8 , 9 , 10 ]]
print ( "The original list : " + str (test_list))
N = 4
res = list ( map ( lambda sub: remove_element(N, sub), test_list))
print ( "The list after deletion of element : " + str (res))
|
Output
The original list : [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]
The list after deletion of element : [[5, 6], [5, 6, 1], [], [8, 9, 10]]
Time Complexity: O(n*m), where n is the number of sublists in the input list and m is the average length of the sublists. This is because the map() function applies the remove_element function to each sublist, which takes O(m) time to execute.
Auxiliary Space: O(n*m), since a new list of lists is created to store the filtered sublists, which has n sublists each of average length m.
Similar Reads
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
7 min read
Remove Multiple Elements from List in Python
In this article, we will explore various methods to remove multiple elements from a list in Python. The simplest way to do this is by using a loop. A simple for loop can also be used to remove multiple elements from a list. [GFGTABS] Python a = [10, 20, 30, 40, 50, 60, 70] # Elements to remove remov
3 min read
Remove last K elements of list - Python
Given a list and an integer K, the task is to remove the last K elements from the list. For example, if the list is [1, 2, 3, 4, 5] and K = 2, the result should be [1, 2, 3]. Letâs explore different methods to remove the last K elements from a list in Python. Using list slicingList slicing is one of
3 min read
Python - Remove empty List from List
In this article, we will explore various method to remove empty lists from a list. The simplest method is by using a for loop. Using for loopIn this method, Iterate through the list and check each item if it is empty or not. If the list is not empty then add it to the result list. [GFGTABS] Python a
2 min read
Python | Get positive elements from given list of lists
Given a list of list, the task is to get only positive element from given list. Below are some ways to solve the above problem. Method #1: Using Iteration C/C++ Code # Python code to get positive # element from list of list # List Initialisation Input = [[10, -11, 222], [42, -222, -412, 99, -87]] Ou
5 min read
Uncommon Elements in Lists of List - Python
We are given two lists of lists and our task is to find the sublists that are uncommon between them. (a sublist is considered uncommon if it appears in only one of the lists and not in both.) For example: a = [[1, 2], [3, 4], [5, 6]] and b = [[3, 4], [5, 7], [1, 2]] then the output will be [[5, 6],
4 min read
Remove an Element from a List by Index in Python
We are given a list and an index value. We have to remove an element from a list that is present at that index value in a list in Python. In this article, we will see how we can remove an element from a list by using the index value in Python. Example: Input: [1, 2, 3, 4, 5], index = 2 Output: [1, 2
3 min read
Python program to Remove the last element from list
Given a list of numbers or strings, the task is to write a Python program to remove the last element from a list. Example: Input : [12,53,11,76,22,21] Output : [12,53,11,76,22] Input : ["abc","gfg","text","python"] Output : ["abc","gfg","text"] The application of this type of problem in day-day prog
3 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 structu
6 min read
Remove common elements from two list in Python
When working with two lists in Python, we may need to remove the common elements between them. A practical example could be clearing out overlapping tasks between two to-do lists. The most efficient way to remove common elements between two lists is by using sets. [GFGTABS] Python a = [1, 2, 3, 4, 5
3 min read