🐍 Python for kids · Class 7 · Lesson 14 of 18 · Free
Lambda Functions – Tiny One-Line Functions
⏱ About 30 minutes · ▶ run real Python here — nothing to install · no sign-in
In this lesson you will
- Write a lambda function and compare it with a def function.
- Sort lists of tuples and dictionaries with a key=lambda.
- Transform and select items with map() and filter().
Sometimes you need a function for just one small job, like "sort these players by runs". Writing a full def with a name feels like too much. A lambda is a tiny function you can write right where you need it.
What is a lambda function in Python?
A lambda is a small function written in one line. It has parameters, a colon, and one expression.
Python works out that expression and returns it automatically. You do not write the word return.
Below, both versions of double do exactly the same job. The lambda is just shorter.
▶def double_def(x):2 return x * 234double = lambda x: x * 25print(double_def(5))6print(double(5))
No variables yet.
▌✏️ Change this code and run it yourself
10 10
💡 Read lambda x: x * 2 as "a function that takes x and gives back x * 2".
Can a lambda take more than one parameter?
Yes. Separate the parameters with commas, just like in def. A lambda can even use if and else in one line.
This one-line if is called a conditional expression. It gives one value or the other.
Keep lambdas short. If the job needs several lines or loops, use a normal def instead.
Room area: 20 Pass Fail
💡 A lambda cannot hold a loop, a print() block or many lines. One expression only.
How do I sort a list with key=lambda?
You learned sorted() earlier. Its key option says what to compare for each item.
Here each player is a tuple of name and runs. key=lambda p: p[1] says: compare the runs.
Add reverse=True to put the biggest first, like a real scoreboard.
▶players = [("Rohit", 45), ("Virat", 82), ("Gill", 60)]2by_runs = sorted(players, key=lambda p: p[1], reverse=True)3for name, runs in by_runs:4 print(name, runs)
No variables yet.
▌✏️ Change this code and run it yourself
Virat 82 Gill 60 Rohit 45
💡 sorted() calls the lambda once for every player, then sorts using the numbers it got back.
How do I sort dictionaries or find the top item with lambda?
A list of dictionaries is common for student records. The key lambda picks one field from each record.
max() and min() also accept key. So max(..., key=...) finds the whole record with the top value.
That is handy, because you get the name and the marks together.
Asha 88 Kabir 95 Neha 79 Topper: Kabir
How do map() and filter() use lambda?
map() runs a function on every item of a list. filter() keeps only the items where the function gives True.
Both give back a lazy object, so wrap them in list() to see the result.
Below we add 18% GST to each price, then keep only the items under ₹150.
▶prices = [100, 250, 40]2with_gst = list(map(lambda p: p * 118 // 100, prices))3print(with_gst)4cheap = list(filter(lambda p: p < 150, with_gst))5print(cheap)
No variables yet.
▌✏️ Change this code and run it yourself
[118, 295, 47] [118, 47]
Is a list comprehension better than map and filter?
You already know list comprehensions. They do the same jobs as map() and filter().
Many Python programmers find comprehensions easier to read. Both styles are correct.
Use whichever is clearer to you, but recognise both when you read other people's code.
[9, 64, 25, 144] [9, 64, 25, 144] [8, 12] [8, 12]
💡 Where lambda really shines is key= in sorted(), max() and min().
🤔 Predict the output
Think like Python: read the code and choose what it prints. You get the reason straight away.
Question 1. What does this print?
add = lambda a, b: a + b
print(add("7", "3"))Question 2. What does this print?
words = ["mango", "fig", "banana"] print(sorted(words, key=lambda w: len(w)))
Question 3. What does this print?
nums = [4, 9, 15, 2] big = list(filter(lambda n: n > 5, nums)) print(big)
🧩 Your challenge
A school has city temperatures stored as (city, temperature) tuples. Print the cities from coolest to hottest, then print the hottest city with max(). Finally, print a list of only the cities above 30 degrees.
Shimla 18 Pune 27 Chennai 31 Delhi 34 Hottest: Delhi Above 30: ['Delhi', 'Chennai']
⚠️ Common mistakes (and how to fix them)
❌ Wrong
square = lambda x: return x * x print(square(4))
SyntaxError: invalid syntax
✅ Right
square = lambda x: x * x print(square(4))
A lambda returns its expression automatically. Writing return inside it is not allowed.
❌ Wrong
nums = [1, 2, 3] print(map(lambda n: n * 10, nums))
No error message – but it prints something like <map object at 0x...> instead of the numbers.
✅ Right
nums = [1, 2, 3] print(list(map(lambda n: n * 10, nums)))
map() and filter() give a lazy object that has not done the work yet. list() makes it produce all the items.
❌ Wrong
players = [("Rohit", 45), ("Virat", 82)]
print(sorted(players, key=lambda p: p[2]))IndexError: tuple index out of range
✅ Right
players = [("Rohit", 45), ("Virat", 82)]
print(sorted(players, key=lambda p: p[1]))Each tuple has only index 0 and 1. The runs are at index 1, so p[2] does not exist.
📌 Remember
- lambda parameters: expression makes a small one-line function.
- A lambda returns its expression automatically; never write return inside it.
- key=lambda tells sorted(), max() and min() what to compare.
- map() changes every item; filter() keeps items where the lambda is True.
- Wrap map() and filter() in list() to see the results.
- Use def for anything longer than one simple expression.
Finished the quiz and the challenge?
Questions students ask
Why is it called lambda?
The name comes from lambda calculus, a branch of mathematics about functions. Lambda is the Greek letter λ.
Should I store a lambda in a variable?
It works, as you saw. But style guides prefer def for named functions. Lambdas are best used directly inside sorted(), map() or filter().
Can a lambda have no parameters?
Yes, like greet = lambda: "Namaste". Calling greet() then returns "Namaste".
How do I sort by two things with lambda?
Return a tuple, like key=lambda s: (s["class"], s["name"]). Python compares the first item, then the second if there is a tie.