Sort List of Floats in Python (2 Examples)


In this tutorial you’ll learn how to sort a list of floats in Python programming.

The table of content is structured as follows:

Sound good? Let’s dive into it!


Exemplifying Data

Let’s first construct some example data to illustrate how to sort a list of float numbers in Python.

float_list = [1.5, 3.3, -0.87, 15.99, -3.0, 0]    # generating an example float list

As seen float_list contains six float numbers as you can understand from the decimal points.


Example 1: Sorting with sort() Method

This example demonstrates how to use the sort() method for sorting a list of floats in ascending and descending order.

float_list.sort()                                 # sorting the list ascending
print(float_list)                                 # printing the sorted list
# [-3.0, -0.87, 0, 1.5, 3.3, 15.99]

When the reverse parameters are set to ‘True’, then the list will be sorted in a descending way.

float_list.sort(reverse=True)                     # sorting the list descending
print(float_list)                                 # printing the sorted list
# [15.99, 3.3, 1.5, 0, -0.87, -3.0]

You can see how the first element (-0.3) and last element (15.99) in float_list in ascending order are now placed in the last and first positions in float_list in descending order.


Example 2: Sorting using sorted() Function

In this example, the sorting will be done with the sorted() function.

print(sorted(float_list))                         # sorting the list and printing
# [-3.0, -0.87, 0, 1.5, 3.3, 15.99]

The difference between the sort() method and the sorted() function is that sort() method doesn’t generate a new list. The sort() method transforms the list into the sorted version, however, the sorted() function returns a new list with the same elements but sorted. This means that the sorted() function doesn’t alter the original list.

See how float_list still takes the same initial values by printing it.

print(float_list)                                 # printing float_list
# [1.5, 3.3, -0.87, 15.99, -3.0, 0]

Once again if the reverse parameter is set to True then the sorting will be in a descending way.

print(sorted(float_list, reverse=True))           # sorting in reverse and printing
# [15.99, 3.3, 1.5, 0, -0.87, -3.0]

You can compare and see that the same results were obtained with Example 1.


Video & Further Resources

Do you need further information on the topics of this page? Then I recommend having a look at the following video on my YouTube channel. I’m demonstrating the contents of this tutorial in the video.


The YouTube video will be added soon.


In addition, you could read the other articles on this homepage:


Summary: This article has demonstrated how to create a sorted list of floats in Python in Python. Please let me know in the comments section if you have any additional questions or comments. Furthermore, please subscribe to my email newsletter for updates on new tutorials.


Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview of his other tasks on Statistics Globe.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.