If you are confused about how to sort a list in python and even if you have no idea about what sorting actually is, do not worry we’ve got you covered.
Here we have tried to compile a guide about how to go about sorting with simple real-life practical examples, which we hope will ease your way in understanding the sorting all the way through.
Before getting started with the actual tutorial, it is important to understand and clear the concepts. If you are already aware of it, feel free to jump to the solution down below.
What Sorting means?
Sorting is the process of arranging data in a particular order so that the readers can analyze the data more easily to obtain a meaningful sense out of it.
Sorting of data makes it easier to apprehend, inspect, and anticipate the data more easily.
The data that is being sorted must have the same type and all of the data & must have the same attribute.
The process of data sorting is generally stationed on the basis of some numeric values, enumeration, or percentages.
The final sorting occurs in ascending order or in descending order, but sometimes can also be sorted out based on some variable value labels.
[su_label type=”success”]Suggested read[/su_label] How to write content to txt file with examples
Some Simple Examples:
Let’s take 5 numbers as:
- 8,17,11,3,25(unsorted)
- 3,8,11,17,25(sorted in ascending order)
- 25,17,11,8,3(sorted in ascending order)
Let us take a more practical example.
Consider a situation where the price of petrol in different countries as of 1st of September 2019 has been depicted:
Country | Price of petrol/liter (in rupees) |
India | 69.26 |
Pakistan | 42.14 |
Sri Lanka | 53.47 |
Bhutan | 62.21 |
Malaysia | 32.19 |
Now in the table above the data regarding the price of petrol per liter has been stored but the data is not sorted.
Country | Price of petrol/liter (in rupees) |
India | 69.26 |
Bhutan | 62.21 |
Sri Lanka | 53.47 |
Pakistan | 42.14 |
Malaysia | 32.19 |
Now in this final table, the country with the highest petrol price has been sorted in the first row, followed by the country with the second-highest petrol price and so on and so forth.
By this, the reader will now be able to understand the order of the countries, without comparing them again and again.
[su_label type=”success”]Suggested read[/su_label] How to read file line by line in python
How to sort a list in python?
Though there are various tools available for sorting (many applications and software where data can be entered and sorted data of any format may be obtained).
But here we will be using a python programming language for the process of sorting data.
sort() in Python
Python, like many OOPs languages (JAVA and C++), comes with an in-built function for sorting data.
This sort()
function can be used to sort the lists in python in both ascending and descending orders. This sort()
function is used for sorting lists.
The list can contain data of the type integers,floating-point number, strings, and any other.
Sorting in ascending order
List of Integers
num_list = [5, 1, 0, 9]
Sorting list of Integers.
num_list.sort() print(num_list)
List of floating-point numbers
decimal_list = [2.01, 2.00, 3.67, 3.28, 1.68]
Sorting list of the floating-point number
decimal_list.sort() print(decimal_list)
Output:
[0, 1, 5, 9 ] [1.68, 2.0, 2.01, 3.28, 3.67]
Sorting in descending order:
List of Integers
num_list = [5, 1, 0, 9]
Sorting list of Integers
num_list.sort(reverse=true) print(num_list)
List of floating-point numbers
decimal_list = [2.01, 2.00, 3.67, 3.28, 1.68]
Sorting list of the floating-point number
decimal_list.sort(reverse=true) print(decimal_list)
Output:
[9, 5, 1, 0] [3.67, 3.28, 2.01, 2.0, 1.68]
Interlude
Syntax:
name_of_list.sort() – sorts in ascending order name_of_list.sort(reverse=True) – sorts in descending order name_of_list.sort(key=…, reverse=…) – sorts according to user’s choice
[su_label type=”success”]Suggested read[/su_label] What are lambda functions in python ?
Conclusion on how to sort a list in python:
So far we have discussed sorting, what it means with some practical examples and we’ve also discussed sorting of lists in python using sort()
function which is predefined in the python library.
Also there are certain algorithms, and one can go around by using one to fulfill its sorting needs in python or in any other programming language.
Sorting algorithms like bubble, selection, insertion, merge, quick, optimize whole sorting operations. They make the runtime, time-complexity, space-complexity of the whole program more efficient.