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: python sort
>>> student_tuples = [
... ('john', 'A', 15),
... ('jane', 'B', 12),
... ('dave', 'B', 10),
... ]
>>> sorted(student_tuples, key=lambda student: student[2])
[('dave', 'B', 10), ('jane', 'B', 12), ('john', 'A', 15)]
Example 3: 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}")
Example 4: sort in python'
arr = [1,4,2,7,5,6]
arr = arr.sort()
arr = arr.sort(reverse = True)
Example 5: python sort
nums = [4,8,5,2,1]
sorted_nums = sorted(nums)
print(sorted_nums)
print(nums)
nums.sort()
print(nums)