phyton sort list code example
Example 1: how do i sort list in python
my_list = [9, 3, 1, 5, 88, 22, 99]
my_list = sorted(my_list, reverse=True)
print(my_list)
my_list = sorted(my_list, reverse=False)
print(my_list)
my_list.sort(reverse=True)
print(my_list)
print(my_list[::-1])
Example 2: how to manually sort a list in python
Numbers = []
iterate = 0
while len(Numbers)<5:
try:
x = int(input("Insert the number you want in the list: "))
Numbers.append(x)
except:
print("The input MUST be a number.")
continue
for iteration_count in range(len(Numbers)):
for j in range(0,len(Numbers)-1):
if (Numbers[j]>Numbers[j+1]):
Numbers[j],Numbers[j+1] = Numbers[j+1],Numbers[j]
print(f"Here are the sorted numbers:{Numbers}")