Well, since set
s inherently dedupe things, your first instinct might be to do set[mylist]
. However, that doesn't quite work:
In [1]: mylist = [[1,2,3], ['a', 'c'], [3,4,5],[1,2], [3,4,5], ['a', 'c'], [3,4,5], [1,2]]
In [2]: set[mylist]
---------------------------------------------------------------------------
TypeError Traceback [most recent call last]
in []
----> 1 set[mylist]
TypeError: unhashable type: 'list'
This is because set
s only work on iterable
s of hashable elements [and since list
s are mutable, they are not hashable].
Instead, you can do this simply for the price of converting your sublists to subtuples:
In [3]: set[[tuple[x] for x in mylist]]
Out[3]: {[1, 2], [1, 2, 3], [3, 4, 5], ['a', 'c']}
Or, if you really need a list of lists again:
In [4]: [list[x] for x in set[[tuple[x] for x in mylist]]]
Out[4]: [[1, 2], [3, 4, 5], ['a', 'c'], [1, 2, 3]]
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.
def
Remove[lst]:
return
[[
list
[i]
for
i
in
{
*
[
tuple
[
sorted
[i]]
for
i
in
lst]}]]
lst
=
[[
1
], [
1
,
2
], [
3
,
4
,
5
], [
2
,
1
]]
print
[Remove[lst]]
Output:
[[1, 2], [3, 4, 5], [1]]
Approach #2 : Using map[] with set and sorted tuples.
def
Remove[lst]:
return
list
[
map
[
list
, [
set
[
map
[
lambda
x:
tuple
[
sorted
[x]], lst]]]]]
lst
=
[[
1
], [
1
,
2
], [
3
,
4
,
5
], [
2
,
1
]]
print
[Remove[lst]]
Output:
[[1, 2], [3, 4, 5], [1]]
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.
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
lst
=
[[
1
], [
1
,
2
], [
3
,
4
,
5
], [
2
,
1
]]
print
[Remove[lst]]
Output:
[[1], [1, 2], [3, 4, 5]]