Q:

how to sort list 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 = [1, 3, 4, 2] 
  
# Sorting list of Integers in ascending in place
numbers.sort() 
  
print(numbers) 
6
strs = ["geeks", "code", "ide", "practice"] 
  
# Sorting list of Integers in ascending 
strs.sort() 
  
print(strs) 
2

    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
List_name.sort()
This will sort the given list in ascending order.
1

New to Communities?

Join the community