Python Lambda Exercises

Let’s check out some exercises that will help understand Lambda better.

Exercise 11-a

Write a lambda function that takes x as parameter and returns x+2. Then assign it to a variable named L.


Supplementary Material: If you are still struggling in understanding lambda or making use of it in a practical sense, check out our Python Lambda Lesson. In that lesson we elaborate on different nuances of lambda through 10+ high quality lambda examples in Python!

Seeing as many examples as possible and practicing is the best way to become instinctively good at Python (or any programming language) concepts and to effortlessly be able to use them in real world applications.

You can start your function as following: lambda x: 
And then write your statement after the colon (:)

L = lambda x: x+2

Exercise 11-b

Write a lambda function which takes z as a parameter and returns z*11


Assign it to variable named: f.


You can start your function as following: lambda z: 
And then write your statement after the colon (:)

f = lambda z: z*11

Exercise 11-c

Write a function which takes two arguments: a and b and returns the multiplication of them: a*b. Assign it to a variable named: f.


You can start your function as following: lambda a, b: 
And then write your statement after the colon (:)

f = lambda a, b: a*b

Exercise 11-d

Using .sort() method, create a lambda function that sorts the list in descending order. Refrain from using the reverse parameter.


(Hint: lambda will be passed to sort method's key parameter as argument)


Please check out Hint 0 below to be informed about a glitch regarding this exercise.


Although 1/x should suffice, test fails in the system probably due to a decimal conflict, so kindly just use 100/x, the mathematical idea to flip the numbers’ value remains the same.

You can type your lambda function after the key parameter in .sort() method:

key=lambda x:…..

1/x can be a useful to create a reverse sorting function.

If 1/x is the output when x is the input that means, greater the x, smaller the 1/x will be.

Based on this logic .sort() will think it’s sorting from smaller to greater as usual but your function sort of tricks it, if you will.

lst.sort(key=lambda x: 100/x)

Exercise 11-e

This time use the sorted() function to sort the list in ascending order with lambda.


sorted() is a builtin function rather than a method. So it will take the list itself as parameter on top of your lambda function.

sorted(lst, key…)

lst = sorted(lst, key=lambda x: x)

 

Exercise 11-f

Using sorted() function and lambda sort the words in the list based on their second letter from a to z.


sorted() is a builtin function rather than a method. So it will take the list itself as parameter on top of your lambda function.

Your lambda function can take x as input and x[1] (second letter) can be the output.
Remember, this is not to actually return second letter as value, it is only the logic for the sorting function.

lst = sorted(lst, key=lambda x: x[1])

Exercise 11-g

Using sorted() function and lambda sort the tuples in the list based on the second items.


sorted() is a builtin function rather than a method. So it will take the list itself as parameter on top of your lambda function.

Your lambda function can take x as input and x[1] (second item) can be the output.
Remember, this is not to actually return second letter as value, it is only the logic for the sorting function.

lst = sorted(lst, key=lambda x: x[1])

Exercise 11-h

Using sorted() function and lambda sort the tuples in the list based on the last character of the second items.


sorted() is a builtin function rather than a method. So it will take the list itself as parameter on top of your lambda function.

Your lambda function can take x as input and x[1] [-1](second letter) can be the output.

x[1][-1] will give the last character of the second item.

lst = sorted(lst, key=lambda x: x[1][-1])

Exercise 11-i

Using sorted() function, reverse parameter and lambda sort the tuples in the list based on the last character of the second items in reverse order.


sorted() is a builtin function rather than a method. So it will take the list itself as parameter on top of your lambda function.

Your lambda function can take x as input and x[1] [-1](second letter) can be the output.

x[1][-1] will give the last character of the second item.

You will need to include the reverse parameter this time:

reverse = True

lst = sorted(lst, key=lambda x: x[1][-1], reverse = True)