Reputation: 13
[["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"].....]
I have got a list like this. I want to remove duplicate elements from nested list in Python where elements should be in any order.
Output should be:-
[["hello", "bye", "start"], ["john", "riya", "tom"]]
3 strings should be present only once in any list. How to achieve this?
Upvotes: 1
Views: 1110
Reputation: 71471
You can use enumerate
:
s = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
new_s = [a for i, a in enumerate(s) if not any(all(c in h for c in a) for h in s[:i])]
Output:
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]
Upvotes: 3
Reputation: 1539
Try this:-
a = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
ls = []
for i in a:
i = sorted(i)
if i not in ls:
ls.append(i)
print(ls)
As u said want to have same output like list then try this tricky method however it won't be pythonic way :-
ls = []
ind = []
for i,j in enumerate(a):
j = sorted(j)
if j not in ls:
ind.append(i)
ls.append(j)
ls1 = [a[x] for x in ind]
print(ls1)
Output:-
[['hello', 'bye', 'start'], ['john', 'riya', 'tom']]
Upvotes: 1
Reputation: 11083
Simply covert in to set
it removes items automatically.
a = [list(i) for i in {frozenset(k) for k in a}]
Upvotes: 10
Reputation: 1121
I think this is a pretty simple solution:
a = [['hello', 'bye', 'start'], ['bye', 'start', 'hello'], ['john', 'riya', 'tom'], ['riya', 'john', 'tom']]
for i in range(len(a)):
a[i].sort()
for i in a:
if i not in b:
b.append(i)
it outputs:
[['bye', 'hello', 'start'], ['john', 'riya', 'tom']]
Upvotes: 0
Reputation: 4983
a simple and naive solution
arr = [["hello", "bye", "start"], ["bye", "start", "hello"], ["john", "riya", "tom"], ["riya","john", "tom"]]
seen, tmp, result = list()
for i in arr:
for j in i:
if j not in seen:
seen.append(j)
tmp.append(j)
if len(tmp) == 3:
result.append(tmp)
tmp = list()
print result
keep in mind this will only append list with 3 elements, so some cases may cause absent elements in the output list (as per your example and instructions given)
Upvotes: 0