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!
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:
- Sort List of datetime Objects in Python
- Sort pandas DataFrame by Multiple Columns in Python
- Access Dictionary within List in Python
- All Python Programming Tutorials
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.
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.
Statistics Globe Newsletter