Built with Tutify.io

    AIStudyLabs

    Python Sort List of Numbers or Integers

    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.

    <a id="h-2-1-sort-list-of-numbers-example"></a>

    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. The sorted() function can be used to create a new list with the elements sorted in ascending order without modifying the original list.

    
    # Create list of numbers
    numbers = [30,20,10,70,50,0]
    print("Original list:",numbers)
    
    # Sorting in ascending order 
    # Using sorted()
    sort_numbers = sorted(numbers)
    print("Sorted list (Ascending):",sort_numbers)
    

    Yields below output.

    2.2 Sort Numbers By Descending Order

    Use param reverse=True to sort list of numbers in descending order. Sorting numbers in descending order can use the sorted() function with the reverse parameter set to True. The sorted() function can be used to create a new list with the elements sorted in descending order without modifying the original list. Here is an example,

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

    Yields below output.

    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.

    <a id="h-3-1-sort-list-of-numbers-in-ascending-order"></a>

    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']
    

    <a id="h-frequently-asked-questions"></a>

    Frequently Asked Questions

    You can use the sorted() function to create a new sorted list or the sort() method to sort the list in place.

    Use the sorted() function with the reverse=True parameter or the sort() method with reverse=True.

    The sorted() function creates a new sorted list, leaving the original list unchanged. The sort() method, on the other hand, modifies the original list in place.

    To sort a list in a custom order, you can use the key parameter of the sorted() function or the sort() method. The key parameter allows you to specify a custom sorting criterion.

    The elements in the list must be comparable for sorting. If you have a mix of data types, consider converting them to a compatible type or using a custom sorting function.

    You can sort a list of dictionaries based on a specific key using the sorted() function or the sort() method, along with the key parameter.

    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 !!

    Related Articles

    References