From the course: Python Functions for Data Science

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Python sorted() function

Python sorted() function - Python Tutorial

From the course: Python Functions for Data Science

Start my 1-month free trial

Python sorted() function

- A powerful built in Python function that is fundamental when working with sequences of data is the function sorted. Sorted takes in a sequence as the required input and can also take in a keyword argument named reverse as an optional input. Now reverse can be set to either True or False. If reverse is not specified in the function call, reverse is set to false by default and ascending order is used to sort. On the other hand, if reverse is set to True in the function call, descending order is used to sort. Sorted returns a new sorted list from the items in the given sequence and does not modify the given sequence itself. To illustrate how the function sorted works I'll be walking through some examples. First, let's say that I have a variable named names containing a list of the names of some people as strings. To sort the names in this list in ascending alphabetical order and save the sorted version in a new variable,…

Contents