Reputation: 219
Given input:
[('is','my','order'), ('my','order', 'is'), ('temp', 'ques'), ('ques','temp')]
Desired output:
[('is','my','order'), ('temp', 'ques')]
In the output, the order of the tuple or the order of the contents of tuple doesn't matter.
Upvotes: 4
Views: 1180
Reputation:
mytuples = [('one','two'), ('two','one)]
myset=set()
for x in mytuples:
myset.update(x)
print(myset)
output {'two', 'one'}
this will give you a set of all the unique values in your dataset. maybe not helpful
so you may have to check each tuple against every other tuple with the same length assuming you want to keep the tuple structure
Upvotes: 0
Reputation: 48120
Because for you order doesn't matter, you can use set
to achieve this:
>>> input_list = [('is','my','order'), ('my','order', 'is'), ('temp', 'ques'), ('ques','temp')]
>>> set(tuple(sorted(l)) for l in input_list)
set([('ques', 'temp'), ('is', 'my', 'order')])
Firstly sort the content of each nested tuple to ensure your set considers tuples with common items as same. Then I am type-casting it again to tuple
because sorted
returns list
which are unhashable. Finally, set
removes duplicate entries of your tuples.
Please refer "set" documentation for more details.
Upvotes: 5
Reputation: 1960
You can sort each of the sublists and then add them to a list, dropping all duplicates
output_list = []
for tup in map(sorted, tuple_list):
if tup not in output_list:
output_list.append(tup)
print(output_list)
Upvotes: 0