Best April deals - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
The key argument
5. The key argument with other functions
Lambdas
The itemgetter() function
Summary

Instruction

The key argument can also be used with some of the other functions we've learned in this course, such as max(). Here's an example:

products = [
  'SuperPower3000', 'maxVoltage 2', 
  'ELECTROKING', 'aMpLiFiEr 52S',
  'AlternatingCurrent', 'el3ctric 2.0'
]

print(max(products)) # maxVoltage 2
print(max(products, key=len)) # AlternatingCurrent

By default, max(products) returns the last string (lexicographically speaking), which is 'maxVoltage 2' in this case. However, we changed the default behavior on the last line, where we specified the len() function as the key. Now, the maximum element is 'AlternatingCurrent', which is the longest string in the list.

Exercise

Write a function named get_mins(input_string_list) that accepts a string list and returns a tuple with three elements:

  1. The first string in default alphabetical order.
  2. The first string in alphabetical order, ignoring the letter case.
  3. The shortest string.

Don't change the input list.

Stuck? Here's a hint!

Use min() three times. The first min() should not use the key argument.