Use itertools
:
import itertools
#to include pairs with same element (i.e. 1-1, 2-2 and 3-3)
>>> ["-".join(pair) for pair in itertools.product(lst, lst)]
['1-1', '1-2', '1-3', '2-1', '2-2', '2-3', '3-1', '3-2', '3-3']
#to exclude pairs with the same element
>>> ["-".join(pair) for pair in itertools.permutations(lst, 2)]
['1-2', '1-3', '2-1', '2-3', '3-1', '3-2']
2
solved Connecting strings in a element without using double for loops Python [duplicate]