Answers for "python sort list of integers"

28

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
1

python sort list of integers

numbers = [1, 5, -2, 4]
# Sort in ascending order, applied in place
numbers.sort()
# Sort in descending order, applied in place
numbers.sort(descending=True)
Posted by: Guest on September-20-2021
2

sort list of numbers python

nums = [48, 35, 32, 5, 5, 16, 5, 16, 28, 29] # Makes a list of numbers

sortedNums = sorted(nums, key=int) # Sorts the numbers and saves it as a variable
print(sortedNums) # Prints the variable
Posted by: Guest on March-30-2021
47

python sort list

# 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']
Posted by: Guest on February-18-2020
1

sort a list numbers in python

numbers = [1, 5, -2, 4]
numbers.sort()
print(numbers)
Posted by: Guest on February-11-2021

Code answers related to "python sort list of integers"

Python Answers by Framework

Browse Popular Code Answers by Language