Python | Remove all occurrences in nested list
The 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.
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)) |
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)) |
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]]
Recommended Posts:
- Python | Remove duplicates from nested list
- Python | Remove all duplicates and permutations in nested list
- Python | Check if a nested list is a subset of another nested list
- Python | Remove duplicate dictionaries from nested dictionary
- Python | Pair and combine nested list to tuple list
- Python | Find maximum length sub-list in a nested list
- Python | Convert a nested list into a flat list
- Python | Convert given list into nested list
- Python | How to copy a nested list
- Python | Intersection of two nested list
- Nested List Comprehensions in Python
- Python | Column wise sum of nested list
- Python | Split nested list into two lists
- Python | Find the sublist with maximum value in given nested list
- Python | Convert list of nested dictionary into Pandas dataframe
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



