Python Sort List of Numbers or Integers

You can use the built-in sorted() function in Python to sort a list of numbers or integer values. This method will return a new list after sorting list. Alternatively, you can also use the sort() function to sort numbers, this updates the sort in place, meaning that it will modify the original list and you won’t need to create a new list.

1. Quick Examples of Sorting List of Numbers

If you are in a hurry, below are some quick examples of sorting list of numbers or integers.


# Below are quick examples

# Example 1: Sort list of numbers
numbers = [30,20,10,70,50,0]
numbers.sort()

# Example 2: Sort the list in descending order
numbers.sort(reverse=True)

# Example 3: Using sorted()
sort_tech = sorted(numbers)

# Example 4: Using sorted() to descending order
sort_tech = sorted(numbers, reverse=True)

# Example 5: Sort the string of numbers
numbers = ["30","20","10","70","50","0"]
numbers.sort(keys=int)

2. Using sorted() to Sort List of Numbers

The sorted() function in Python is used to sort a sequence (such as a list, tuple) of numbers in ascending order. The function returns a new sorted list, leaving the original sequence unchanged.

2.1. Sort List of Numbers Example

Here is an example of how you can use the sorted method to order a list of numbers in ascending order.


# Sort list of numbers
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
sort_numbers = sorted(numbers)
print("Sorted:",sort_numbers)

# Output:
# Original:  [30, 20, 10, 70, 50, 0]
# Sorted: [0, 10, 20, 30, 50, 70]

2.2 Sort Numbers By Descending Order

Use param reverse=True to sort list of numbers in descending order. Here is an example,


# Sort list of numbers on descending order
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
sort_numbers = sorted(numbers, reverse=True)
print("Sorted:",sort_numbers)

# Output:
# Original:  [30, 20, 10, 70, 50, 0]
# Sorted: [70, 50, 30, 20, 10, 0]

3. Using sort() to Order List of Numbers

By using the list.sort() function you can order a list of numbers in ascending order, it takes key and reverse as parameters, key is a function to specify the sorting criteria(s), and reverse is a boolean value that specifies whether the list should be sorted in ascending (False) or descending (True) order. The default is reverse=False.

3.1. Sort List of Numbers in Ascending Order

Here is an example of how you can use the sort method to order a list of numbers in ascending order.


# Sort list of numbers using sort()
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
numbers.sort()
print("Sorted:",numbers)

# Output:
# Original:  [30, 20, 10, 70, 50, 0]
# Sorted: [0, 10, 20, 30, 50, 70]

3.2 Sort Numbers in Descending Order

To sort a list of numbers in descending order, you can use the sort() method with the reverse argument set its value to reverse=True.


# Sort list of numbers by descending order
numbers = [30,20,10,70,50,0]
print("Original: ",numbers)
numbers.sort(reverse=True)
print("Sorted:",numbers)

# Output:
# Original:  [30, 20, 10, 70, 50, 0]
# Sorted: [70, 50, 30, 20, 10, 0]

4. Sort List of String with Numbers

By using the above functions you can also sort the list of strings with numbers in ascending or descending order. Here, you need to convert the list to strings to numbers first. hence, I will use the key param of these functions to convert to int.

Note that, here the sorted list will be of string type but the data is numerically sorted by ascending order.


# Sort list of strings with numbers
# Using sorted()
numbers = ["30","20","10","70","50","0"]
print("Original: ",numbers)
sprt_numbers = sorted(numbers, key=int)
print("Sorted:",sprt_numbers)

# Using sort()
numbers = ["30","20","10","70","50","0"]
print("Original: ",numbers)
numbers.sort(key=int)
print("Sorted:",numbers)

# Output:
# Original:  ['30', '20', '10', '70', '50', '0']
# Sorted: ['0', '10', '20', '30', '50', '70']

Conclusion

In this article, I have explained how to sort a list of numbers in python, First, I have covered using sorted() function and then list.sort() to order the numbers in ascending and descending order.

Happy Learning !!

NNK

SparkByExamples.com is a Big Data and Spark examples community page, all examples are simple and easy to understand and well tested in our development environment Read more ..

Leave a Reply