-1

I want to create a function that would create n different tuples of an n-element tuple and append them to a list:

for n=2 I could simply use:

def change_order(tuple):
   tup = tuple[::-1]
   return tup

And I would append both tuples to a list But for bigger tuples, this function is not valid anymore. I would want to get for example when n=3:

(1,2,3)-->[(1,2,3),(3,1,2),(2,3,1)]

Basically each element moves one place to the right.

Can someone help me with this?

Progman
  • 16,827
  • 6
  • 33
  • 48
Seraline
  • 3
  • 1

1 Answers1

0

Create a tuple of n-elements and rotate it n-times:

n = 3

def rotate(t, n):
    return t[-n:] + t[:-n]

r = tuple(range(1, n + 1))
print([rotate(r, i) for i in range(n)])

Prints:

[(1, 2, 3), (3, 1, 2), (2, 3, 1)]
Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91