Python | Remove all occurrences in nested listThe task of removal of 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. Problem can be removing all occurrences of nested list. Let’s discuss certain ways in which this problem can be solved. Show
Table of Contents
Method #1 : Using list comprehension The list comprehension can be used as a shorter method to the recommended longer method in 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 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]] 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 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]] Article Tags : Python Python Programs Python list-programs Read Full Article Python Program to Flatten a Nested ListIn this example, you will learn to make a flattened list from a nested list in Python. To understand this example, you should have the knowledge of the following Python programming topics:
“remove entire sublist from nested list python” Code Answerhow to remove element from nested list in python python by Alive Aardvark on Sep 19 2020 Comment 0 Add a Grepper Answer Python answers related to “remove entire sublist from nested list python”
Python queries related to “remove entire sublist from nested list python”
“remove nested list from list python” Code Answerhow to remove element from nested list in python python by Alive Aardvark on Sep 19 2020 Comment 0 Add a Grepper Answer Python answers related to “remove nested list from list python”
Python queries related to “remove nested list from list python”
|