Answers for "sort in list"

29

how do i sort list in python

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]
Posted by: Guest on February-23-2020
2

how to sort list python

a_list = [3,2,1]
a_list.sort()
Posted by: Guest on September-19-2020
1

how to sort a list in python

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]
Posted by: Guest on July-04-2020
0

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)):
        #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}")
Posted by: Guest on April-23-2020

Python Answers by Framework

Browse Popular Code Answers by Language