Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a specific range of elements in a list?

Suppose I have a list,

lst = [5, 3, 5, 1, 4, 7]

and I want to get it ordered from the second element 3 to the end.

I thought I could do it by:

lst[1:].sort()

But, this doesn't work.

How can I do it?

like image 469
Synapse Avatar asked Nov 27 '22 22:11

Synapse


2 Answers

lst = lst[0:1] + sorted(lst[1:])
like image 111
Jacob Avatar answered Jan 05 '23 03:01

Jacob


lst = [5, 3, 5, 1, 4, 7]
lst[1:] = sorted(lst[1:])
print(lst) # prints [5, 1, 3, 4, 5, 7]
like image 30
Jasmijn Avatar answered Jan 05 '23 03:01

Jasmijn