cats = ['Maine Coon', 'Tabby', 'Siamese', 'Garfield', 'Sylvester']
cats
['Maine Coon', 'Tabby', 'Siamese', 'Garfield', 'Sylvester']
# Your code here
# Your code here
# Your code here
# Your code here
# Your code here
Hint: look at the docstring's optional parameter, key=
.
You can then define a special function called a lambda function on the fly like this:
lambda thing: thing*-1
or lambda x: operation(x)
Like a for loop you can create any variable name after calling lambda
.
After calling lambda and a variable name, you then always use a colon :
.
To the right of the colon is whatever you want your function to do with your variable.
More completely:
numbers = [5,3,7,82, 17]
numbers.sort()
numbers
[3, 5, 7, 17, 82]
numbers.sort(key=lambda x: x*-1) #Effectively sorts in descending order by taking the inverse
numbers
[82, 17, 7, 5, 3]
numbers.sort(key=lambda num: abs(11-num)) #Try to explain what this does.
numbers
[7, 17, 5, 3, 82]
# Your code here