Lesson 3 · 3 of 20
Lambdas, sorted, map and filter · Python
Sort, filter and transform data in Python with lambda functions, sorted(key=...), map and filter, using a product catalogue.

A lambda is a small nameless function: lambda price: round(price * 1.21, 2) takes a price and returns it with VAT. It allows a single expression and no return. The program stores it in vat to show it off, though you would normally write it right where it is used.
sorted(products, key=lambda p: p["price"]) sorts the dictionaries by price: key is the function that says what to compare for each item. It returns a new list and leaves products untouched.
To sort by several criteria, the key returns a tuple: (-p["price"], p["name"]) sorts from most to least expensive (the minus sign flips the order) and breaks ties by name. That is why Desk comes before Monitor. max() and min() accept the same key=.
filter(function, data) keeps the items for which the function returns True (here, the ones in stock) and map(function, data) applies the function to every item. Both are lazy: they do nothing until you loop over them, which is why the program wraps them in list() or a comprehension.
map takes any function, not just lambdas: map(str.upper, ...) uses the string method and map(vat, ...) reuses the stored lambda.
Code · main.py
products = [ {"name": "Laptop", "price": 899.0, "stock": 4}, {"name": "Mouse", "price": 15.5, "stock": 120}, {"name": "Monitor", "price": 189.9, "stock": 0}, {"name": "Webcam", "price": 45.0, "stock": 12}, {"name": "Desk", "price": 189.9, "stock": 3},]# A lambda is a small function with no name: arguments : expressionvat = lambda price: round(price * 1.21, 2)print("Laptop with VAT:", vat(899.0))# sorted(key=...) decides what to comparecheapest = sorted(products, key=lambda p: p["price"])print("By price:", [p["name"] for p in cheapest])# Tuples as keys: price high to low, then name A to Zranked = sorted(products, key=lambda p: (-p["price"], p["name"]))print("Ranked:", [p["name"] for p in ranked])# max and min also accept key=print("Most stock:", max(products, key=lambda p: p["stock"])["name"])# filter keeps the items for which the function returns Truein_stock = filter(lambda p: p["stock"] > 0, products)print("In stock:", [p["name"] for p in in_stock])# map applies a function to every itemnames = map(str.upper, (p["name"] for p in products))print("Labels:", list(names))print("With VAT:", list(map(vat, [15.5, 45.0])))Output · python3 main.py
Laptop with VAT: 1087.79 By price: ['Mouse', 'Webcam', 'Monitor', 'Desk', 'Laptop'] Ranked: ['Laptop', 'Desk', 'Monitor', 'Webcam', 'Mouse'] Most stock: Mouse In stock: ['Laptop', 'Mouse', 'Webcam', 'Desk'] Labels: ['LAPTOP', 'MOUSE', 'MONITOR', 'WEBCAM', 'DESK'] With VAT: [18.75, 54.45]
Key points
lambda args: expressioncreates a one-line function.sorted(data, key=...)sorts by whatever the key returns.- A tuple key sorts by several criteria.
filterandmapare lazy: pass them tolist()to see them.- A comprehension is often clearer than
mapandfilter.
Exercise
Print the product names sorted by their stock value (price * stock), highest first.