Python | Remove repeated sublists from given list
Last Updated :
17 Apr, 2023
Given a list of lists, write a Python program to remove all the repeated sublists (also with different order) from given list. Examples:
Input : [[1], [1, 2], [3, 4, 5], [2, 1]]
Output : [[1], [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.
Python3
# Python3 program to Remove repeated
# unordered sublists from list
def Remove(lst):
return ([list(i) for i in {*[tuple(sorted(i)) for i in lst]}])
# Driver code
lst = [[1], [1, 2], [3, 4, 5], [2, 1]]
print(Remove(lst))
Output:[[1, 2], [3, 4, 5], [1]]
Time Complexity: O(n), where n is the number of elements in the list
Auxiliary Space: O(n), where n is the number of elements in the list
Approach #2 : Using map() with set and sorted tuples.
Python3
# Python3 program to Remove repeated
# unordered sublists from list
def Remove(lst):
return list(map(list, (set(map(lambda x: tuple(sorted(x)), lst)))))
# Driver code
lst = [[1], [1, 2], [3, 4, 5], [2, 1]]
print(Remove(lst))
Output:[[1, 2], [3, 4, 5], [1]]
Time Complexity: O(n) where n is the number of elements in the list
Auxiliary Space: O(1), extra space is not required
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.
Python3
# Python3 program to Remove repeated
# unordered sublists from list
def Remove(lst):
res = []
check = set()
for x in lst:
hsh = tuple(sorted(x))
if hsh not in check:
res.append(x)
check.add(hsh)
return res
# Driver code
lst = [[1], [1, 2], [3, 4, 5], [2, 1]]
print(Remove(lst))
Output:[[1], [1, 2], [3, 4, 5]]
Time Complexity: O(nlogn)
Auxiliary Space: O(n)
Similar Reads
Python | Remove given element from the list Given a list, write a Python program to remove the given element (list may have duplicates) from the given list. There are multiple ways we can do this task in Python. Let's see some of the Pythonic ways to do this task. Example: Input: [1, 8, 4, 9, 2] Output: [1, 8, 4, 2] Explanation: The Element 9
7 min read
Remove Duplicate Strings from a List in Python Removing duplicates helps in reducing redundancy and improving data consistency. In this article, we will explore various ways to do this. set() method converts the list into a set, which automatically removes duplicates because sets do not allow duplicate values.Pythona = ["Learn", "Python", "With"
3 min read
Python | Remove tuples from list of tuples if greater than n Given a list of a tuple, the task is to remove all the tuples from list, if it's greater than n (say 100). Let's discuss a few methods for the same. Method #1: Using lambda STEPS: Initialize a list of tuples: ini_tuple = [('b', 100), ('c', 200), ('c', 45), ('d', 876), ('e', 75)]Print the initial lis
6 min read
Python | Duplicate substring removal from list Sometimes we can come to the problem in which we need to deal with certain strings in a list that are separated by some separator and we need to remove the duplicates in each of these kinds of strings. Simple shorthands to solve this kind of problem is always good to have. Let's discuss certain ways
7 min read
Python - Remove duplicate words from Strings in List Sometimes, while working with Python list we can have a problem in which we need to perform removal of duplicated words from string list. This can have application when we are in data domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using set() + split() + loop The
6 min read