Binge On Code

Jun 8, 2023

156 readers

How to sort python list – part one

There is nothing as common as sorting when it comes to working with data. This tutorial will give you two part guide on how to sort python list.

When you work with python data,you will find yourself at one point or another needing to sort python list, based on some given criteria maybe. This simple guide will explain to you how you can go about this, based on the data type you are working with.

The first part of this simple sort python list guide will explain what we will be working with, and then the second part will now take a look at the available methods for sorting.

Another thing to note is that we will be using the built in python sorted method for this sorting in python mini-series.

Okay, if you are ready, let’s get started!

What we will work with.

We will focus on basically sorting numbers and one of the most commonly used python data structures – dictionary.

Part one of this simple sort python list guide will introduce how sort works and the basics of it, by using a simple example.

Then, there will be Part two, that you can find in our Python tutorials, which will now dive deeper where we will learn hot to sort python list of python data structures – a list of dictionaries!

How python sorting works.

To be able to understand Part two of this guide - sorting python data structures - you need to understand how to sort a python list using python sorted method.

For that reason, we begin with a simple example:

simple_number_list = [1, 4, 3, 6, 2, 7, 10, 8, 9, 5]

This is some random number. So, you can sort it in two directions, either in ascending or descending order.

By default, python sorted method uses ascending sorting order. This is one thing to remember.

So:

sorted_list = sorted(simple_number_list)
print(sorted_list)

Will sort our list from smallest to the largest. The print method will display this:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

Well, what if that is not our intention? Well, there is the reverse argument which we can pass into python sorted method and well, you guessed it right, reverse the sorting order.

sorted_list = sorted(simple_number_list, reverse=True)
print(sorted_list)

Now, the print method will display this:

[10, 9, 8, 7, 6, 5, 4, 3, 2, 1]

By specifying:

reverse=True

you are telling the sorted method to:

  • 1. Sort the results.
  • 2. Now reverse the list of what you just sorted.

So, you may wonder what:

reverse=False

would do, well, it would do nothing since that is the default.

Another IMPORTANT key takeaway from this brief introduction is that sorting does not happen in place, you will need to assign the results to another variable, or just the same variable, so that you can have the sorted list.

If you would have just had this:

simple_number_list = [1, 4, 3, 6, 2, 7, 10, 8, 9, 5]
sorted(simple_number_list, reverse=True)
print(sorted_list)

Then you would still have the original list, with no updates. So always remember, python sorted returns a new sorted list of what was passed into it.

Conclusion.

Well, this concludes this simple introduction on how to sort python list. You have learnt:

  • python sorted() method.
  • sorted method sorts in ascending order by default.
  • sorted method can accept reverse parameter to sort either in ascending or descending order.
  • sorting DOES NOT happen in place, you need to assign the return value of sorted method, which is a sorted list.

Well, tune in for the next part of this tutorial, where we will be sorting a list of python data structures called dictionaries.

Happy Coding!

Related Articles

How to remove an item from a python list

There are a couple of ways to remove item from python list. This simple guide will focus mainly on the built in python methods for doing this!

Jun 8, 2023

Views 142

How to sort python list – part two

This is the second part of this awesome short series of how to sort python list. We will be sorting a list of python dictionaries! So let’s do this!

Jun 8, 2023

Views 275

Python list - How to insert an item into a python list

Arrays otherwise known as lists in python are common data structures in python. Python list contains items and it can be modified by adding more items to it.

Jun 8, 2023

Views 589

Angular JavaScript React CSS Django Python React Native Next JS State Management Django Rest Framework Unity