Sunday, November 17, 2024
Google search engine
HomeLanguagesPython | Remove all occurrences in nested list

Python | Remove all occurrences in nested list

The task of removing an element generally doesn’t pose any challenge, but sometimes, we may have a more complex problem than just removing a single element or performing removal in just a normal list. The problem can be removing all occurrences of the nested list. Let’s discuss certain ways in which this problem can be solved.

Method #1: Using list comprehension The list comprehension can be used as a shorter method to the recommended longer method in the normal way of loops to perform this task in which we just check for a match and reconstruct the list without the target list element. 

Python3




# Python3 code to demonstrate
# Remove all occurrences in nested list
# using list comprehension
 
# initializing list
test_list = [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
 
# initializing list to delete
del_list = [4, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing delete list
print("The list to be deleted is : " + str(del_list))
 
# using list comprehension
# Remove all occurrences in nested list
res = [i for i in test_list if i != del_list]
 
# print result
print("The list after removal of list element : " + str(res))


Output

The original list : [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
The list to be deleted is : [4, 5]
The list after removal of list element : [[1, 2, 3], [8, 9], [10, 11]]

Time complexity: O(n), where n is the number of elements in the test_list. 
Auxiliary space: O(m), where m is the number of elements in the result list, res. 

Method #2 : Using filter() + partial() + operator.ne The task can also be performed using the above functions. The filter function performs filtering, and returns the partial list by partial function and not equal condition is imposed using the operator.ne method. 

Python3




# Python3 code to demonstrate
# Remove all occurrences in nested list
# using filter() + partial() + operator.ne
from functools import partial
from operator import ne
 
# initializing list
test_list = [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
 
# initializing list to delete
del_list = [4, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing delete list
print("The list to be deleted is : " + str(del_list))
 
# using filter() + partial() + operator.ne
# Remove all occurrences in nested list
res = list(filter(partial(ne, del_list), test_list))
 
# print result
print("The list after removal of list element : " + str(res))


Output

The original list : [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
The list to be deleted is : [4, 5]
The list after removal of list element : [[1, 2, 3], [8, 9], [10, 11]]

Time Complexity: O(n * m) where n is the number of sublists in the main list and m is the number of elements in each sublist.
Auxiliary Space: O(n) where n is the number of sublists in the main list as we are creating a new list to store the result.

Method #3: Using while loop+remove() method

Python3




# Python3 code to demonstrate
# Remove all occurrences in nested list
 
# initializing list
test_list = [[4, 5, 8], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
 
# initializing list to delete
del_list = [4, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing delete list
print("The list to be deleted is : " + str(del_list))
 
while(del_list in test_list):
    test_list.remove(del_list)
# print result
print("The list after removal of list element : " + str(test_list))


Output

The original list : [[4, 5, 8], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
The list to be deleted is : [4, 5]
The list after removal of list element : [[4, 5, 8], [1, 2, 3], [8, 9], [10, 11]]

Time Complexity: O(n^2) where n is the number of elements in the test_list. 
Auxiliary Space: O(1) as no extra space is required.

Method #4: Using nested loop

  • Initialize an empty list res.
  • Loop through each element lst in test_list.
  • Initialize an empty list new_lst.
  • Loop through each element elem in lst.
  • If elem is not in del_list, append elem to new_lst.
  • After looping through all elements in lst, append new_lst to res.
  • After looping through all elements in test_list, return res.

Python3




# initializing list
test_list = [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
 
# initializing list to delete
del_list = [4, 5]
 
# printing original list
print("The original list : " + str(test_list))
 
# printing delete list
print("The list to be deleted is : " + str(del_list))
 
# using nested loops to remove all occurrences of del_list
res = []
for lst in test_list:
    new_lst = []
    for item in lst:
        if item not in del_list:
            new_lst.append(item)
    if new_lst:
        res.append(new_lst)
 
# print result
print("The list after removal of list element : " + str(res))


Output

The original list : [[4, 5], [1, 2, 3], [4, 5], [8, 9], [10, 11]]
The list to be deleted is : [4, 5]
The list after removal of list element : [[1, 2, 3], [8, 9], [10, 11]]

Time complexity: O(n^2) where n is the length of test_list.
Auxiliary space: O(n) where n is the length of test_list.

RELATED ARTICLES

Most Popular

Recent Comments