Python | Remove duplicates from nested list
The task of removing duplicates many times in recent past, but sometimes when we deal with the complex data structure, in those cases we need different techniques to handle this type of problem. Let’s discuss certain ways in which this task can be achieved.
Method #1 : Using sorted() + set()
This particular problem can be solved using the above functions. The idea here is to sort the sublist and then removing the like elements using the set operations which removes duplicates.
# Python3 code to demonstrate # removing duplicate sublist # using set() + sorted() # initializing list test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original list print("The original list : " + str(test_list)) # using set() + sorted() # removing duplicate sublist res = list(set(tuple(sorted(sub)) for sub in test_list)) # print result print("The list after duplicate removal : " + str(res)) |
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
Method #2 : Using set() + map() + sorted()
The task performed by the list comprehension in the above method can be modified using the map function using lambda functions to extend the logic to each and every sublist.
# Python3 code to demonstrate # removing duplicate sublist # using set() + map() + sorted() # initializing list test_list = [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] # printing original list print("The original list : " + str(test_list)) # using set() + map() + sorted() # removing duplicate sublist res = list(set(map(lambda i: tuple(sorted(i)), test_list))) # print result print("The list after duplicate removal : " + str(res)) |
The original list : [[1, 0, -1], [-1, 0, 1], [-1, 0, 1], [1, 2, 3], [3, 4, 1]] The list after duplicate removal : [(-1, 0, 1), (1, 3, 4), (1, 2, 3)]
Recommended Posts:
- Python | Remove all duplicates and permutations in nested list
- Python | Remove all occurrences in nested list
- Python | Remove Duplicates from a List
- Python | Ways to remove duplicates from list
- Python | Remove consecutive duplicates from list
- Python | Sort given list by frequency and remove duplicates
- 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
- Remove all duplicates from a given string in Python
- Python | Remove duplicates in Matrix
- Python | Convert a nested list into a flat list
- Python | Remove all duplicates words from a given sentence
- Python groupby method to remove all consecutive duplicates
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.



