Python Lambda · Exercises
Core Python

Python Lambda Exercises

4 exercises that build on the Python Lambda Functions lesson, from easy to hard. Each one is checked in your browser when you press Check. Stuck? Use the hint before peeking at the solution.

What this lesson covers: A lambda is a small anonymous function written as a single expression, lambda parameters: expression, that returns the expression's value and is usually passed straight to another function. By the end you can write lambda functions, say how they differ from def, use them as key functions for sorted, min and max, store them in a dictionary, avoid the late-binding trap, and decide when a normal def is clearer. You will practise: Sort names by their last letter; Predict the operation table; Fix the multipliers; Rank students with a tie-break.

0 / 4 completed

Python Lambda practice exercises

1. Sort names by their last letter

Write code Easy

Write a function sort_by_last(names) that returns a new list of the names sorted by their last letter. Use sorted() with a lambda as the key. Names that share a last letter keep their original order.

Then print the result for ["Asha", "Ben", "Cara", "Dan"].

Your code
def sort_by_last(names):
    # return a new list sorted by the last letter of each name
    pass

print(sort_by_last(["Asha", "Ben", "Cara", "Dan"]))

2. Predict the operation table

Predict the output Medium

A dictionary stores three lambdas and the program calls them by name. Type exactly what it prints, then press Check.

Read this code
rules = {
    "double": lambda n: n * 2,
    "negate": lambda n: -n,
    "label": lambda n: "even" if n % 2 == 0 else "odd",
}
for name in ["double", "negate", "label"]:
    print(name, rules[name](7))
print(rules["label"](rules["double"](3)))

3. Fix the multipliers

Fix the bug Medium

multipliers should hold three functions that multiply a value by 2, by 3 and by 4. The program should print [20, 30, 40], but it prints [40, 40, 40] because every lambda uses the last value of factor. Fix the lambda so each function remembers its own factor.

Your code
multipliers = []
for factor in [2, 3, 4]:
    multipliers.append(lambda value: value * factor)

print([m(10) for m in multipliers])

4. Rank students with a tie-break

Write code Hard

Write rank_students(students). Each student is a dictionary with "name" and "score". Return a list of the names ordered like this:

  1. highest score first,
  2. students with the same score in alphabetical order of name,
  3. students whose score is None (absent from the exam) at the very end, also in alphabetical order.

Use one sorted() call with a lambda key that returns a tuple, and do not change the original list. Print the ranking for class_list.

Your code
def rank_students(students):
    # return the names, best score first, None scores last
    pass

class_list = [
    {"name": "Ben", "score": 88},
    {"name": "Asha", "score": 88},
    {"name": "Dan", "score": None},
    {"name": "Cara", "score": 95},
]
print(rank_students(class_list))

Next lessonPython Recursion

More Python Lambda Functions examples · All Python exercises