Lambda functions let you write small, quick functions in one line. Regular functions are better for longer, clearer tasks.
Lambda vs regular functions in Python
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
Python
Lambda function: lambda arguments: expression Regular function: def function_name(arguments): statements return value
Lambda functions can only have one expression and no statements.
Regular functions can have many lines and multiple statements.
Examples
Python
add = lambda x, y: x + y print(add(3, 5))
Python
def add(x, y): return x + y print(add(3, 5))
Python
square = lambda x: x * x print(square(4))
Python
def square(x): result = x * x return result print(square(4))
Sample Program
This program shows how to use lambda and regular functions to square a list of numbers.
Python
numbers = [1, 2, 3, 4, 5] squares_lambda = list(map(lambda x: x * x, numbers)) # Using regular function def square(x): return x * x squares_regular = list(map(square, numbers)) print('Squares with lambda:', squares_lambda) print('Squares with regular function:', squares_regular)
Important Notes
Lambdas are anonymous, meaning they usually don't have a name unless you assign one.
Regular functions are easier to debug because they have names and can have comments.
Use lambdas for simple tasks; use regular functions for complex logic.
Summary
Lambdas are short, one-line functions for simple tasks.
Regular functions use def and can have many lines and statements.
Choose lambdas for quick use and regular functions for clarity and reuse.
Practice
1. Which of the following best describes a
lambda function in Python?easy
Solution
Step 1: Understand lambda function definition
A lambda function is defined using thelambdakeyword and is anonymous (no name).Step 2: Compare with regular functions
Regular functions usedefand can have multiple statements, unlike lambdas which are single expressions.Final Answer:
A short, anonymous function defined with thelambdakeyword. -> Option BQuick Check:
Lambda = short anonymous function [OK]
Hint: Lambdas are short and nameless functions [OK]
Common Mistakes:
- Thinking lambdas can have multiple statements
- Confusing lambda with regular def functions
- Believing lambdas must return None
2. Which of the following is the correct syntax for a lambda function that adds 5 to its input?
easy
Solution
Step 1: Recall lambda syntax
A lambda function is written aslambda parameters: expressionwithoutdeforreturn.Step 2: Check each option
lambda x: x + 5 matches the correct syntax:lambda x: x + 5. Others have syntax errors or usedef.Final Answer:
lambda x: x + 5 -> Option CQuick Check:
Lambda syntax = lambda params: expression [OK]
Hint: Lambda uses colon, no def or return [OK]
Common Mistakes:
- Including 'def' or 'return' in lambda
- Missing colon after parameters
- Using parentheses incorrectly
3. What is the output of the following code?
add = lambda x, y: x + y
def add_func(x, y):
return x + y
print(add(3, 4))
print(add_func(3, 4))medium
Solution
Step 1: Understand lambda and regular function behavior
Bothadd(lambda) andadd_func(regular) add two numbers and return the sum.Step 2: Evaluate the print statements
Callingadd(3, 4)andadd_func(3, 4)both return 7, so output is two lines with 7.Final Answer:
7 7 -> Option AQuick Check:
Both add functions return sum = 7 [OK]
Hint: Both lambda and def return same result if logic matches [OK]
Common Mistakes:
- Thinking lambda returns string concatenation
- Confusing output with error messages
- Assuming lambda can't return values
4. Identify the error in this code snippet:
multiply = lambda x, y:
x * y
print(multiply(2, 3))medium
Solution
Step 1: Check lambda syntax rules
Lambda functions must have their expression on the same line as thelambdakeyword and parameters.Step 2: Analyze the code structure
The code places the expressionx * yon the next line, causing an IndentationError.Final Answer:
IndentationError because lambda body is on new line -> Option DQuick Check:
Lambda body must be on same line [OK]
Hint: Lambda body must be on same line as parameters [OK]
Common Mistakes:
- Placing lambda body on next line
- Adding colon after lambda parameters
- Thinking lambda can't take multiple arguments
5. You want to sort a list of tuples by the second item using a function. Which is the best way to write the key function?
hard
Solution
Step 1: Understand sorting with key functions
Thekeyparameter expects a function that takes one argument and returns the value to sort by.Step 2: Evaluate options for correctness and efficiency
def get_second(t): return t[1] list.sort(key=get_second(t)) defines a regular function but incorrectly calls get_second(t) (causing NameError: 't' not defined). list.sort(key=lambda t: t[1]) uses a lambda inline, which is concise and common. list.sort(key=lambda t t[1]) has syntax error (missing colon). list.sort(key=get_second()) callsget_second()instead of passing the function.Step 3: Choose best practice
Using a lambda inline is best for simple, one-time use functions.Final Answer:
list.sort(key=lambda t: t[1]) -> Option AQuick Check:
Lambda inline for simple key function [OK]
Hint: Use lambda inline for simple sort keys [OK]
Common Mistakes:
- Forgetting colon in lambda
- Calling function instead of passing it
- Using multi-line def when lambda suffices
