Aan de slagGa gratis aan de slag

Using a lambda expression as an argument

Let's pass lambda expressions as arguments to functions. You will deal with the list .sort() method. By default, it sorts numbers in increasing order. Characters and strings are sorted alphabetically. The method can be defined as .sort(key=function). Here, key defines a mapping of each item in the considered list to a sortable object (e.g. a number or a character). Thus, the items in a list are sorted the way sortable objects are.

Your task is to define different ways to sort the list words using the key argument with a lambda expression.

Deze oefening maakt deel uit van de cursus

Practicing Coding Interview Questions in Python

Cursus bekijken

Praktische interactieve oefening

Probeer deze oefening eens door deze voorbeeldcode in te vullen.

# Sort words by the string length
words.sort(key=lambda s: ____)
print(words)
Code bewerken en uitvoeren