Python sorted() function


(Sponsors) Get started learning Python with DataCamp's free Intro to Python tutorial. Learn Data Science by completing interactive coding challenges and watching videos by expert instructors. Start Now!


The sorted()  built-in function allows us to sort the data. It accepts an iterable and returns a sorted list containing the items from the iterable. By default, it sorts in ascending order.

The syntax of sorted()  function is as follows:

Syntax: sorted(iterable, key=None, reverse=False)

Parameter Description
iterable (required) Iterable to sort like string, list, dictionary, tuple etc.
key (optional) It refers to the single argument function to customize the sort order. The function is applied to each item on the iterable. By default, this argument is set to None.
reverse (optional)  A boolean flag to reverse the sorting order. It defaults to False .

If the items in the iterable is a string, they will be sorted in alphabetical order. On the other hand, if they are numbers, they will be sorted in numerical order.

Here is an example:

Note that the sorted()  returns a new list containing items from the iterable. It doesn’t change the original iterable in the process.

Here are some other examples showing how sorted()  works with other Python types.

sorted() with string

Notice that in the result of the first sorted()  call A come before a. This is because the ASCII value of A  is 65 and that of a  is 97. For the same reason space character ( ' ' ), ASCII value of 32 comes before A.

sorted() with tuple

sorted() with dictionary

Customizing the sort order using the key named argument.

From the preceding section, we know that if we apply sorted()  function on a list of strings, we will get a list of strings sorted in alphabetical order.

What if we want to sort by length of the string instead of its alphabetical order?

This is where the key named arguments come into the play.

To sort by string length set the key named argument to len function as follows:

At times you may want to make the sorting case-insensitive. We can easily achieve this by setting key named argument to str.lower function.

Here is another example which uses a custom function to sort a list of strings based on the number of vowels it contains.

You can also use sorted() on user-defined objects.

If you now call sorted()  on emp_list  you will get an error like this:

Note: The above code will not raise any error in Python 2. Instead, it will sort the Employee  objects based upon the id returned by the id() built-in function.

This happens because Python doesn’t know how to compare Employee objects. We can tell Python how to compare object by implementing special methods like __lt__() , __gt__() and so on, in the Employee class.

Instead, of definition special methods, we can explicitly tell the sorted() function how to sort Employee objects using the key named argument.


Other Tutorials (Sponsors)

This site generously supported by DataCamp. DataCamp offers online interactive Python Tutorials for Data Science. Join over a million other learners and get started learning Python for data science today!


Leave a Reply

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