Passing in a key Argument
00:19 If that doesn’t make sense, don’t worry. This is a bit more complicated than anything we’ve covered so far. Let’s start with a basic example and sort a group of words based on their length. Open up your interpreter and make a new list of words.
you’d see that everything would be returned in alphabetical order—with the capital letter first, which we’ll talk about later. So this time, you’re going to call
sorted(), pass in
words, and then for
key, set this equal to
len, which is how you return the length of a string.
When you call
sorted() with a function in the
key parameter, each element will be sent to this function and make almost a temporary list to sort off of. To get an idea of what this would look like, you can kind of make a list comprehension and say something like
[len(word) for word in words]. And, of course, it helps if you have the right number of brackets.
So, this is pretty cool, but it does have some caveats. For example, a function used as the
key argument may only take one argument. Each element will be passed in one by one, so if a function requires multiple arguments, this won’t work. To see what I mean, you can go ahead and define a new function and just call that
add(). You’ll take an
x and a
and then for the
key you just say
add, you’ll run into an error. And you can see here that it’s missing one argument,
y. So when
sorted() tried to go through this list, it passed in
x and then
x and then
x, and it never knew what to put in for
y. Now, even though you can only pass in one argument to the
key function, this doesn’t mean that you can’t pass in a list or dictionary. In fact, most of the times I use a
key argument is when I’m dealing with a list of lists or a list of dictionaries.
The other limitation with
key is that the function has to be able to handle all the data types in the iterable. So if you were to say something like
values_to_cast and set this equal to the string value of a couple numbers—we’ll just say
'3', but then type out
Even with these limits, adding a
key function is very powerful. You can use any builtin or define your own functions, as long as they only take in one argument and return the value that you want to sort off of.
Now look at this. So this time, what
reverse_word() did was flip each of these words around and then the sort was performed off of that. In this case, it was looking at the last letter of each word, so you have
05:15 Hopefully by now, you’re seeing that you have a lot of different options for customizing how you sort your iterables. The only issue is if you’re defining all these different functions for relatively straightforward tasks, you can get your code pretty well cluttered.
So, let’s try to use a
lambda to replace this
reverse_word() function that you had previously defined. Go ahead and call
sorted(), pass in the
words, and this time for
key, you’re going to type out
lambda and then put an
x here with a colon (
:), and then just say your slice notation like before.
Let’s try to run this, and you can see that you end up with the same list as when you sorted it with that
reverse_word() function. Now, lambdas can look very complicated if you’ve never seen one before, but what you basically have here is that for each
x that you pass into this
lambda, you’re going to take that
x and then reverse it.
Even though they can seem pretty complicated, lambdas are a great way to keep your code nice and neat, especially in cases like this where you only need the function to perform one small operation to your data and then you never need it again. Hopefully by now, you can see that with passing in a function as the
key argument, you can completely change the way
sorted() is looking at the elements in your list. These can definitely be tricky at first, especially if you’re not used to working with
lambda functions or defining your own functions in a way that will return a value that you want to sort off of.
I highly recommend taking some time to practice this and see what kind of new ways you can sort the list that you have. All right! Now you should have a pretty good understanding of how to use the Python built-in function
sorted() and a few different ways to use it. In the next video, we’re going to talk about the
.sort() method and see how you can achieve similar results using that. Thanks for watching.
Become a Member to join the conversation.