0

I have an algorithm that outputs a list in a specific order, for example:

[0 4 3 2 1 5]

I want to reorder the list to start with the element '1' and keep the sequence, so my output would be:

[1 5 0 4 3 2]

I've searched and tried different possibilities but I'm still struggling with it.

How can I make this work?

1 Answers1

3
lst = [0, 4, 3, 2, 1, 5]

to rotate it into position:

i = lst.index(1)
lst = lst[i:] + lst[:i]
user2390182
  • 72,016
  • 6
  • 67
  • 89
  • Can make one-liner with Walrus operator: `lst = lst[(p:=lst.index(1)):] + lst[:p]` – DarrylG Jan 25 '21 at 15:19
  • I am well aware, as seen in my comments. However, it only works in the most recent Python versions and does not make it more readable or performant. – user2390182 Jan 25 '21 at 15:23