How to sort the part of a list in Python

1 Answer

0 votes
my_list = [15, 6, 19, 8, 3, 7, 9, 1, 4]

# Sort a specific portion (e.g., indices 2 to 6)
my_list[2:7] = sorted(my_list[2:7]) # 3, 7, 8, 9, 19

print(my_list)



'''
run:

[15, 6, 3, 7, 8, 9, 19, 1, 4]

'''

 



answered Aug 11, 2025 by avibootz
...