Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Generating lists from other lists while keeping the order

I have a list in the form of [2, 1, 4, 3].
Is there a way to produce all of the possible sequences of the list values while keeping the same order? To further clarify, I am expecting something like this:

[1, 4, 3, 2], 
[4, 3, 2, 1], 
[3, 2, 1, 4],
[2, 1, 4, 3]

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>>> data = [4, 3, 2, 1]
[4, 3, 2, 1]
>>> l = len(data)
>>> for i in range(l):
...     print(data[l - i:] + data[0:l - i])
...
[4, 3, 2, 1]
[1, 4, 3, 2]
[2, 1, 4, 3]
[3, 2, 1, 4]

Or if we want to capture them rather than print them:

[data[len(data)-i:] + data[0:len(data)-i] for i in range(len(data))]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading