You can use itertools.permutations
:
import itertools
input = [3, 8, 2]
final_list = ["{}-{}".format(*i) for i in itertools.permutations(input, 2)]
Output:
['3-8', '3-2', '8-3', '8-2', '2-3', '2-8']
However, if you want all operations up-to and including the length of the list, you can try this:
final_list = list(itertools.chain(*[['-'.join(["{}"]*b).format(*i) for i in itertools.permutations(input, b)] for b in range(2, len(input)+1)]))
Output:
['3-8', '3-2', '8-3', '8-2', '2-3', '2-8', '3-8-2', '3-2-8', '8-3-2', '8-2-3', '2-3-8', '2-8-3']
Edit: for all possible operands:
import re
def tokenize(s):
converter = {"-":lambda x, y:x-y, '+':lambda x, y:x+y}
total = 0
stack = re.findall('\d+|[\-\+]', s)
operator = None
for i in stack:
if i.isdigit():
if not operator:
total += int(i)
else:
total = converter[operator](total, int(i))
operator = None
else:
operator = i
return total
new_list = set(list(itertools.chain(*list(itertools.chain(*[[[''.join([''.join('{}'+i) for i in b]+['{}']).format(*c) for b in itertools.permutations(['-', '+'], len(c)-1)] for c in itertools.permutations(input, x)] for x in range(2, len(input)+1)])))))
final_list = {tokenize(a):a for a in new_list}
new_final_list = [b for a, b in final_list.items()]
Output:
['3-2', '8-3', '8-2', '8-3+2', '8-2+3', '8+2', '8+3', '2-8', '3-8', '3+2-8', '2-3']