Home > Python > How to Sort a List in Python?

How to Sort a List in Python?

In this tutorial, you will learn about the python sort() method and how you can use it to sort a list type sequence in python.

Python list is the most versatile sequence type and you can create it using the [] brackets. It stores the collections of data and maintains the order even if any collection is deleted. Python list sequence is mutable, you can add/view items in the list.

The sort() method sorts the python list in ascending order by default. You can also specify the order of the list that you want.

So, let’s get started using the python sort() method to make list in ascending or descending order.

Sort a List in Python Using sort() Method

By default sort() method returns the list in ascending order. This means the lower order elements alphabetically come first before the higher order elements.

And if you want to print the list in descending order then you have to pass the argument reverse=True. It will print the list of elements from higher order to lower order.

Syntax

The first parameter is used to print the list in ascending/descending order and the second parameter is used when you have any specific sorts of conditions.

Python sort() Method Examples

Let’s see some examples of using the sort() method to sort the list of numbers, sort the list of strings, and also examples of custom sort criteria.

Sort a List of Strings

If you have a list of string elements then the sort() method sorts the list according to alphabetical order.

Output
['Aman', 'Kumar', 'Ramesh', 'Sushil', 'Vindu']

Now if you want to print the list in reverse alphabetically order, pass the argument reverse=True in sort() method. It will print the list in reverse order.

Output
['Vindu', 'Sushil', 'Ramesh', 'Kumar', 'Aman']

Sort a List of Numbers

Now let’s see the example of a numbers list. This list will have all numbers. So when we used the sort() method with the numbers list, it will sort the numbers from smallest to largest.

Output
[245, 345, 466, 768, 908]

To sort the number list in reverse order from largest to smallest, use the reverse=True parameter in the sort() method. It will print the list of numbers from higher to lower order.

Output
[908, 768, 466, 345, 245]

Sort a List of String with Custom Sorting

In this example, we will see how we can use the key argument in sort() method. This argument we used when we want to sort based on the specific condition of logic.

Let’s say we have a list of strings and each string has a different length of character. And we want to sort the list based on the string character’s length. See the following example code.

Sort the list by the length of the values:

Output
['Mini', 'Pooja', 'Khushi', 'Davinder', 'Pakaj Kumar']

Let’s take another example of the key argument when you have a list of dictionaries and you want to sort the list based on the dictionary value.

Sort a list of dictionaries based on the “roll_no” value of the dictionaries:

Output
[
    {'name': 'Pooja', 'roll_no': 12},
    {'name': 'Pakaj Kumar', 'roll_no': 23},
    {'name': 'Khushi', 'roll_no': 34},
    {'name': 'Davinder', 'roll_no': 45},
    {'name': 'Mini', 'roll_no': 78}
]

Sort a List of Disctionaries using Lambda Expression

Python has a feature lambda expression to use in short-hand defining the function without the name. That function is called the anonymous function.

Syntax

Let’s understand this, as you see above in the key arguments examples, we have defined the function that refers to the key argument.

So in the lambda expression example, we will not define any function. We will use the lambda expression with key argument.

Basically, the above syntax of a lambda expression is equivalent to the following function.

Let’s take the same example of the list of students’ dictionaries and we will sort the student based on the ‘roll_no’ using the lambda expression and also in reverse order.

Output
[
    {'name': 'Mini', 'roll_no': 78},
    {'name': 'Davinder', 'roll_no': 45},
    {'name': 'Khushi', 'roll_no': 34},
    {'name': 'Pakaj Kumar', 'roll_no': 23},
    {'name': 'Pooja', 'roll_no': 12}
]

That’s it!!!

Conclusion

So in this tutorial, you have learned about the sort() method of python and how you can use it to sort the list in python.

You also learned about how you can sort the list of strings and list of numbers in ascending or descending order passing the reverse argument with True|False value.

You also see how you can sort with custom sorting criteria like you have a list of tuples or dictionaries and want to sort based on those values.

Hope it is helpful for you and if you have any queries please ask me in the comment section, I will respond to you as soon as possible.

Photo of author

About Aman Mehra

Hey there! I'm Aman Mehra, a full-stack developer with over six years of hands-on experience in the industry. I've dedicated myself to mastering the ins and outs of PHP, WordPress, ReactJS, NodeJS, and AWS, so you can trust me to handle your web development needs with expertise and finesse. In 2021, I decided to share my knowledge and insights with the world by starting this blog. It's been an incredible journey so far, and I've had the opportunity to learn and grow alongside my readers. Whether you're a seasoned developer or just dipping your toes into the world of web development, I'm here to provide valuable content and solutions to help you succeed. So, stick around, explore the blog, and feel free to reach out if you have any questions or suggestions. Together, let's navigate the exciting world of web development!

Leave a Comment