I tried to get the all unique pair combinations from a list.
Here is what I have done so far,
import itertools
# Unique Combination Pairs for list of elements
def uniqueCombinations(list_elements):
l = list(itertools.combinations(list_elements, 2))
s = set(l)
# print('actual', len(l), l)
return list(s)
Test Case
sample = ["apple", "orange", "banana", "grapes", "mango"]
uniqueCombinations(sample)
Output
[('apple', 'banana'),
('orange', 'mango'),
('orange', 'grapes'),
('apple', 'grapes'),
('orange', 'banana'),
('apple', 'mango'),
('grapes', 'mango'),
('apple', 'orange'),
('banana', 'mango'),
('banana', 'grapes')]
The input will be unique strings of list. Assume that duplicate elements will not be passed.
Is there a better / more elegant / more accurate way to do this??
uniqueCombinations(['apple', 'orange', 'apple'])
returns[('orange', 'apple'), ('apple', 'apple'), ('apple', 'orange')]
. Is this the output you are expecting for this case? \$\endgroup\$list_elements
can contains duplicates. \$\endgroup\$list_elements
you get('banana', 'orange')
but lose('orange', 'banana')
. The challenge is poorly defined. If so why are you duplicating output with['apple', 'orange', 'apple']
? \$\endgroup\$