0

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

I want to start from number: 4

So how can I sort the list like:

sorted_my_list = [4, 5, 6, 7, 8, 9, 10, 1, 2, 3]

Fleur Lolkema
  • 35
  • 1
  • 3

1 Answers1

2

You could probably use the index() function on the list to find the position of the desired position/number. But I don't know of the side effects.

my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
starter = 4

result = my_list[my_list.index(starter):] + my_list[:my_list.index(starter)]

Is this something to your liking?

And if you need to sort the two section, add sorted() to them, altho I don't see the point of this.

result = sorted(my_list[my_list.index(starter):]) + sorted(my_list[:my_list.index(starter)])

Final edit I think. If you need to sort the list first, and then split on the 4 you would have to sort the entire list/data before hand, to get the split down the list to be correct. Otherwise you'd end up with incorrect orders if the numbers were un-sorted before the split (my_list = [1, 2, 4, 3, 5, 6, 7] for instance)

my_list = sorted([1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
starter = 4

result = my_list[my_list.index(starter):] + my_list[:my_list.index(starter)]
Torxed
  • 22,866
  • 14
  • 82
  • 131