user70107
0
Q:

how to manually sort a list in python

# sort() will change the original list into a sorted list
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
# Output:
# ['a', 'e', 'i', 'o', 'u']

# sorted() will sort the list and return it while keeping the original
sortedVowels = sorted(vowels)
# Output:
# ['a', 'e', 'i', 'o', 'u']
32
my_list = [9, 3, 1, 5, 88, 22, 99]

# sort in decreasing order
my_list = sorted(my_list, reverse=True) 
print(my_list)

# sort in increasing order
my_list = sorted(my_list, reverse=False) 
print(my_list)

# another way to sort using built-in methods
my_list.sort(reverse=True)  
print(my_list)

# sort again using slice indexes
print(my_list[::-1])

# Output
# [99, 88, 22, 9, 5, 3, 1]
# [1, 3, 5, 9, 22, 88, 99]
# [99, 88, 22, 9, 5, 3, 1]
# [1, 3, 5, 9, 22, 88, 99]
6
a_list = [3,2,1]
a_list.sort()
1
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)):
        #This acts like a counting method
        for j in range(0,len(Numbers)-1):
            #It swaps each element for each iteration (j is just a random variable)
            if (Numbers[j]>Numbers[j+1]):
                Numbers[j],Numbers[j+1] = Numbers[j+1],Numbers[j]
print(f"Here are the sorted numbers:{Numbers}")
1

    cars = ['Ford', 'BMW', 'Volvo']


    cars.sort(reverse=True) 
1
vowels = ['e', 'a', 'u', 'o', 'i']
vowels.sort()
4

    cars = ['Ford', 'BMW', 'Volvo']


    cars.sort() 
1
l=[1,3,2,5]
l= sorted(l)
print(l)
#output=[1, 2, 3, 5]
#or reverse the order:
l=[1,3,2,5]
l= sorted(l,reverse=True)
print(l)
#output=[5, 3, 2, 1]
1
old_list = [3,2,1]
old_list.sort()
1
x = [2, 8, 1, 4, 6, 3, 7]  
  
print ("Sorted List returned :"),  
print (sorted(x))  
  
print ("\nReverse sort :"),  
print (sorted(x, reverse = True))
0

New to Communities?

Join the community