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 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
# Python3 code to demonstrate# Removing element from list of lists# using list comprehension# initializing listtest_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]# printing original listprint("The original list : " + str(test_list))# initializing Number to deleteN = 4# using list comprehension# Removing element from list of listsres = [[ele for ele in sub if ele != N] for sub in test_list]# print resultprint("The list after deletion of element : " + str(res)) |
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
# Python3 code to demonstrate# Removing element from list of lists# using list comprehension + list slicing# initializing listtest_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]# printing original listprint("The original list : " + str(test_list))# initializing Number to deleteN = 4# using list comprehension + list slicing# Removing element from list of listsfor sub in test_list: sub[:] = [ele for ele in sub if ele != N]# print resultprint("The list after deletion of element : " + str(test_list)) |
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]]
Method: Using enumerate function
Python3
test_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]N = 4res = [[ele for j,ele in enumerate(sub) if ele != N] for i,sub in enumerate(test_list)]print(res) |
[[5, 6], [5, 6, 1], [], [8, 9, 10]]
Method: 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): # Loop through each sublist for sub in lst: # Remove element from sublist sub.remove(element)# Initialize list of liststest_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]# Print original listprint("The original list :", test_list)# Remove elementremove_element(test_list, 4)# Print resultprint("The list after deletion of element :", test_list)#This code is contributed by Edula Vinay Kumar Reddy |
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: 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 #base condition sublist=[] for i in oldlist[start]: if i==element:pass #checking for element else:sublist.append(i) newlist.append(sublist) #appending oldlist element to new list return remove_element(start+1,oldlist,newlist,element)#Driver codetest_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]print('The Original list: ',test_list)element=4print('The List after deleted 4 in every nested list:',remove_element(0,test_list,[],element)) |
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 : Using filter()
Python3
test_list = [[4, 5, 6], [5, 6, 4, 1], [4], [4, 8, 9, 10]]N = 4print('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))#This code is contributed by Jyothi pinjala. |
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)


Please Login to comment...