Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Reviewing Python's Built-in sorted()

00:00 In the previous lesson, you had the list points and you used the list method .sort() to reorder these items in place. This means that you did not create a new list.

00:11 Instead, the items were reordered within the same list, within the same object. However, Python also has the built-in function sorted(), and you can call sorted() and pass the list points as an argument.

00:27 And sorted() returns a new list with the values from the original list points, but reordered. An important difference between the list method .sort() and the built-in function sorted() is that the list method .sort() does not create a new list.

00:46 It changes the values within the same list. The built-in function sorted(), however, does not modify, does not mutate the original list. Instead, it returns a new list.

00:58 You can confirm this by showing points, which is the original list, the same values in the original order.

01:06 There is another important difference between the list method .sort() and the built-in function sorted(). You can have a look at help() for the built-in function and the signature will show you that sorted() accepts an iterable, any data type, which is an iterable, which does not have to be a list.

01:27 For example, if you pass a string, let’s create a string “python”. A string is iterable, sorted() will take that string and it’ll return a list. sorted() always returns a list it doesn’t matter what the input argument is.

01:45 However, the list contains the characters in “python”, arranged in alphabetical order. And as a final example, you can create a list names with Jill, Janet, Jim, and Jonathan.

02:00 I’m using different names to the previous lesson and you’ll see why in a second.

02:05 You can call sorted() using names, the list you’ve just created as an argument, and this will return a new list with the same names, but in alphabetical order.

02:17 Since the list names contains strings, sorted(), we’ll sort them out in alphabetical order with Janet coming first, followed by Jill, Jim, and finally, Jonathan. And as a reminder, names has not changed.

02:32 The original list has not mutated. sorted(), creates a new list with reordered values.

02:39 But, what if you want to sort these names using a different rule? For example, you want to sort them from shortest to longest. In that case, you can call the same built-in function sorted() with names as an argument but you can also pass a second argument and you can use the named argument key.

03:00 So key is one of the parameters in sorted() which accepts a function.

03:05 Let’s use len(), the built-in function, which gives you the length of a data type, in this case, the strings. So the way sorted() works when you use the key parameter is that every item from the list names is passed to the function len() in this case, and the value of len() is used to sort the values.

03:29 When you call this function, you can see that the list created has Jim in first place because Jim is the shortest of these names. Then Jill with four letters, then Janet and finally Jonathan, which is the longest name from these four names.

03:44 Therefore, you can decide what rule you want to use when you sort a list or any other iterable using sorted().

03:54 The list method .sort() also has the parameter key. However, in the rest of the course, you’ll be dealing with dictionaries and therefore the list method .sort() won’t be useful.

04:05 However, the built-in function sorted() will, so you can focus on sorted() for the rest of this course.

04:13 Any iterable can be reordered using Python’s built-in function sorted(). This function returns a list, whatever type is used as an argument its return value is always a list with the ordered elements.

04:29 You can also use the key parameter, which allows you to sort an iterable using a user-defined rule.

Become a Member to join the conversation.