Python offers some compact tools for small jobs that would otherwise need several lines. A lambda is a tiny function written in one expression. A comprehension builds a new list (or set or dictionary) from an existing one in a single, readable line.
Lambda functions
A lambda is written as lambda parameters: expression. It returns the value of the expression automatically; there is no return keyword.
square = lambda x: x * x
add = lambda a, b: a + b
print(square(9))
print(add(150, 250))81 400
That is equivalent to:
def square(x):
return x * x
def is clearer. Lambdas really shine when passed directly into another function.Lambdas as sort keys
The most common use of a lambda is the key argument of sorted(), sort(), min() and max().
products = [("Unga", 210), ("Sugar", 160), ("Cooking oil", 395), ("Salt", 40)]
by_price = sorted(products, key=lambda p: p[1])
print(by_price)
most_expensive = max(products, key=lambda p: p[1])
print(most_expensive[0])[('Salt', 40), ('Sugar', 160), ('Unga', 210), ('Cooking oil', 395)]
Cooking oilThe lambda tells Python: "for each product p, compare using p[1]", which is the price.
map() and filter()
map(function, items) applies a function to every item. filter(function, items) keeps only the items for which the function returns True. Both return lazy objects, so wrap them in list() to see the result.
prices_usd = [10, 25, 40]
prices_kes = list(map(lambda p: p * 130, prices_usd))
print(prices_kes)
ages = [12, 19, 25, 15, 31]
adults = list(filter(lambda a: a >= 18, ages))
print(adults)[1300, 3250, 5200] [19, 25, 31]
List comprehensions
A list comprehension creates a list from another sequence in one line:
new_list = [expression for item in sequence]
Compare a normal loop with the comprehension that does the same thing:
numbers = [1, 2, 3, 4, 5]
squares = []
for n in numbers:
squares.append(n * n)
print(squares)
squares = [n * n for n in numbers]
print(squares)[1, 4, 9, 16, 25] [1, 4, 9, 16, 25]
Read it left to right: "make a list of n * n, for each n in numbers."
Adding a condition
Put an if at the end to keep only some items.
marks = [45, 78, 62, 30, 91, 55]
passed = [m for m in marks if m >= 50]
print(passed)
names = ["kamau", "ATIENO", "Mwende"]
clean = [name.title() for name in names]
print(clean)[78, 62, 91, 55] ['Kamau', 'Atieno', 'Mwende']
if...else inside the expression
To transform every item in one of two ways, put a conditional expression at the front.
marks = [45, 78, 62, 30]
results = ["Pass" if m >= 50 else "Fail" for m in marks]
print(results)['Fail', 'Pass', 'Pass', 'Fail']
Note the difference: if at the end filters; if...else at the front chooses a value.
Dictionary and set comprehensions
The same idea works with curly braces.
towns = ["Nyeri", "Kisii", "Malindi"]
lengths = {town: len(town) for town in towns}
print(lengths)
words = ["chai", "Chai", "CHAI", "kahawa"]
unique = {w.lower() for w in words}
print(sorted(unique)){'Nyeri': 5, 'Kisii': 5, 'Malindi': 7}
['chai', 'kahawa']A practical example
Given a list of M-Pesa style transactions, find the total received:
transactions = [
{"type": "received", "amount": 1500},
{"type": "sent", "amount": 700},
{"type": "received", "amount": 2300},
{"type": "sent", "amount": 450},
]
received = sum(t["amount"] for t in transactions if t["type"] == "received")
print("Total received: KES", received)Total received: KES 3800
Writing the comprehension directly inside sum() without square brackets creates a generator expression. It works the same way but does not build a temporary list.
for loop is usually easier to read.Exercises
Complete the lambda that triples a number.
triple = x: x * 3Build a list of the doubled numbers.
doubled = [n * 2 n in numbers]Next: Python Classes and Objects.
SKOPE