I do this:
from itertools import chain, combinations, permutations
import itertools
i = [5, 6, 7]
x = list(combinations(i, 2))
x.append((x[1][0],))
x = list(itertools.chain.from_iterable(x))
to take all the possible permutations but in a flattened list where each pair of adjacent elements corresponds to a permutation.
This gives:
[5, 6, 5, 7, 6, 7, 5]
where the pairs of adjacent elements in this list are all the permutations:
(5,6), (6,5), (5,7), (7,6), (6,7), (7,5)
instead of the classic list of tuples with permutations:
[(5, 6), (5, 7), (6, 5), (6, 7), (7, 5), (7, 6)]
Is there any function or way which does this in a more concise and/or quick way than my code?