🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·🇬🇧 Limited Time — UK Only·🎓 Free Learning for 1 Month·🤖 Free AI Training Included·📚 4,000+ Lessons · 35,000+ Quizzes·🏆 GCSE Mocks · Olympiad Papers·⚡ Selected Students Only · Limited Places·🎁 Free Value Worth £2,000·
🐍 Python

Lambda (small functions)

📚 What are Lambda Functions? A lambda is a tiny, anonymous function written in one line: lambda arguments: expression. It returns the result of the expression automatically. Lambdas are perfect when you need a quick function to pass to map(), filter(), or sorted() without writing a full def.

5 min 10 XP Lesson 19 of 21
Lambda (small functions)
🌐

Appy Says…

Sometimes you need a quick, throwaway function for just one job — like sorting a list by the second element, or filtering items on the fly. Writing a full def block feels like overkill. That's exactly when lambda saves the day.

📖

What is a Lambda Function?

A lambda is an anonymous (unnamed) function defined in a single expression. It can take any number of arguments but has only one expression as its body — the result of which is automatically returned.

  • Syntax: lambda arguments: expression
  • Example: lambda x: x * 2 — doubles its input
  • Assign: double = lambda x: x * 2; double(5)10
  • Multiple args: lambda x, y: x + y
  • Most useful as an argument to sorted(), map(), filter()
  • Sort by second element: sorted(pairs, key=lambda p: p[1])
🎮

Think of it like a one-time Minecraft command block

A full def function is like a named tool in your hotbar — you use it over and over. A lambda is like a command block you place for a single trigger: it runs, does its one job, and you move on. No need to name it.

⚙️

How It Works

  • 1. lambda x: x * 2 creates a function object on the spot
  • 2. It has no name unless you assign it to a variable
  • 3. The expression after : is automatically returned — no return keyword needed
  • 4. Commonly passed directly: sorted(data, key=lambda item: item['score'])
  • 5. map(lambda x: x**2, numbers) applies the function to every item
  • 6. filter(lambda x: x > 0, numbers) keeps only items where the function returns True
🌍

Real-World Examples

  • Sort a leaderboard by score: sorted(players, key=lambda p: p['score'], reverse=True)
  • Filter out inactive users: list(filter(lambda u: u['active'], users))
  • Double all prices: list(map(lambda p: p * 2, prices))
  • Sort files by size: sorted(files, key=lambda f: f.size)
  • GUI button callbacks: btn.command = lambda: print('clicked')
💡

Key Facts

  • Lambdas are function objects — they can be stored in variables, passed as arguments, and returned from functions
  • They cannot contain statements (if/else/for/while are not allowed inside — only expressions)
  • PEP 8 advises: if you assign a lambda to a name, just use def instead — double = lambda x: x*2 should be def double(x): return x*2
  • Lambdas shine as the key argument for sorted(), min(), max()
⚠️

Watch Out!

Don't overuse lambdas for complex logic. If your lambda needs an if/else (ternary is OK: lambda x: 'yes' if x > 0 else 'no'), or is longer than one line, replace it with a proper def function — your future self will thank you.

📌

Remember

Use lambdas for short, throwaway functions — especially as the key in sorted(). For anything complex, use a named def.

What You Learned

  • Lambda creates an anonymous one-line function: lambda args: expression
  • Best used as inline key functions for sorted(), map(), filter()
  • Unlocks: clean sorting by custom keys, compact data transformations, functional programming patterns

Key Facts

  • Lambdas are function objects — they can be stored in variables, passed as arguments, and returned from functions
  • They cannot contain statements (if/else/for/while are not allowed inside — only expressions)
  • PEP 8 advises: if you assign a lambda to a name, just use def instead — double = lambda x: x*2 should be def double(x): return x*2
  • Lambdas shine as the key argument for sorted(), min(), max()

Real-World Examples

• Sort a leaderboard by score: <code>sorted(players, key=lambda p: p['score'], reverse=True)</code> • Filter out inactive users: <code>list(filter(lambda u: u['active'], users))</code> • Double all prices: <code>list(map(lambda p: p * 2, prices))</code> • Sort files by size: <code>sorted(files, key=lambda f: f.size)</code> • GUI button callbacks: <code>btn.command = lambda: print('clicked')</code>

Remember

Use lambdas for short, throwaway functions — especially as the key in sorted(). For anything complex, use a named def.

Quick Quiz

1 / 2

lambda is for?