-1

Take the list [1,2,3], I would like to be able to find all ordered combinations of this list without repetition that are the same length as this list. In my case, I need to be able to generate (1,2,3),(1,3,2),(2,1,3),(2,3,1),(3,1,2),(3,2,1). How can I go about doing this? I cannot seem to find a function within itertools to allow for this to happen.

I have tried to use other parts of itertools to generate all the possible lists of n items, but found this scales poorly for large n.

  • "*this scales poorly for large n*": how large? This is not the function that scales poorly but rather the number of combinations that explodes. – mozway Nov 07 '22 at 21:10
  • @mozway I fixed this duplicate closure. Please pay more attention next time. – Karl Knechtel Feb 28 '23 at 22:01

1 Answers1

1

itertools.permutations is your friend:

>>> from itertools import permutations
>>> list(permutations([1,2,3]))
[(1, 2, 3), (1, 3, 2), (2, 1, 3), (2, 3, 1), (3, 1, 2), (3, 2, 1)]
Alberto Garcia
  • 324
  • 1
  • 11