๐Ÿ‡ฌ๐Ÿ‡ง 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?