Given a list of lists, write a Python program to remove all the repeated sublists (also with different order) from given list.
Input : [, [1, 2], [3, 4, 5], [2, 1]] Output : [, [1, 2], [3, 4, 5]] Input : [['a'], ['x', 'y', 'z'], ['m', 'n'], ['a'], ['m', 'n']] Output : [['a'], ['x', 'y', 'z'], ['m', 'n']]
Approach #1 : Set comprehension + Unpacking
Our first approach is to use set comprehension with sorted tuple. In every iteration in the list, we convert the current sublist to a sorted tuple, and return a set of all these tuples, which in turn eliminates all repeated occurrences of the sublists and thus, remove all repeated rearranged sublists.
[[1, 2], [3, 4, 5], ]
Approach #2 : Using map() with set and sorted tuples.
[[1, 2], [3, 4, 5], ]
With maintaining order –
Approach #3 : Using sorted tuple as hash
First, we initialize an empty list as ‘res’ and a set as ‘check’. Now, For each sublist in the list, convert the sublist to sorted tuple and save it in ‘hsh’. Then check if hsh is present in check or not. If not, append the current sublist to ‘.res’ and ‘hsh’ to ‘check’. This way it would be easier to maintain the order to sublists.
[, [1, 2], [3, 4, 5]]
- Python | Remove elements of list that are repeated less than k times
- Python | Remove all sublists outside the given range
- Python | Remove sublists that are present in another sublist
- Python | Print all sublists of a list
- Python | Count unique sublists within list
- Python | Split a list into sublists of given lengths
- Python | Count the sublists containing given element in a list
- Python | Sort all sublists in given list of strings
- Python | Sort list of lists by the size of sublists
- Python | Split list of strings into sublists based on length
- Python | Remove all values from a list present in other list
- Python list remove()
- Python | Remove last K elements of list
- Python | Remove given element from the list
- Python | Remove Duplicates from a List
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.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.