Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Why lambda functions are used
๐ Scenario: Imagine you have a list of numbers and you want to quickly create a new list with each number doubled. You want to do this without writing a full function because it's a simple task.
๐ฏ Goal: You will learn how to use a lambda function to double numbers in a list using the map() function.
๐ What You'll Learn
Create a list called numbers with the values 1, 2, 3, 4, 5
Create a lambda function that doubles a number
Use the map() function with the lambda to double each number in numbers
Convert the result of map() to a list called doubled_numbers
Print the doubled_numbers list
๐ก Why This Matters
๐ Real World
Lambda functions are used when you need a quick, small function for simple tasks like transforming data in lists.
๐ผ Career
Many jobs require processing data quickly and cleanly. Lambda functions help write concise code for data manipulation and functional programming.
Progress0 / 4 steps
1
Create the list of numbers
Create a list called numbers with these exact values: 1, 2, 3, 4, 5
Python
Hint
Use square brackets [] to create a list and separate numbers with commas.
2
Create a lambda function to double a number
Create a lambda function called double that takes one argument x and returns x * 2
Python
Hint
A lambda function is written as lambda arguments: expression.
3
Use map() with the lambda function
Use the map() function with the double lambda and the numbers list. Convert the result to a list called doubled_numbers
Python
Hint
The map() function applies a function to each item in a list.
4
Print the doubled numbers
Print the list doubled_numbers to see the doubled values
Python
Hint
Use print() to show the list on the screen.
Practice
(1/5)
1. Why are lambda functions commonly used in Python?
easy
A. To improve the speed of the program by compiling code
B. To define large and complex functions with many lines
C. To replace all regular functions in a program
D. To create small, unnamed functions quickly for simple tasks
Solution
Step 1: Understand the purpose of lambda functions
Lambda functions are designed to create small, unnamed functions quickly, usually for simple tasks.
Step 2: Compare with other options
Options A, B, and C describe uses that do not match lambda functions' purpose.
Final Answer:
To create small, unnamed functions quickly for simple tasks -> Option D
Quick Check:
Lambda functions = quick unnamed functions [OK]
Hint: Lambda = quick small function without a name [OK]
Common Mistakes:
Thinking lambda can replace all functions
Believing lambda is for complex logic
Confusing lambda with performance optimization
2. Which of the following is the correct syntax for a lambda function that adds two numbers x and y?
easy
A. lambda x, y: x + y
B. def lambda(x, y): return x + y
C. lambda (x, y) { return x + y }
D. function lambda(x, y) => x + y
Solution
Step 1: Recall lambda syntax in Python
Lambda functions use the syntax: lambda parameters: expression.
Step 2: Check each option
lambda x, y: x + y matches the correct syntax. Options B, C, and D use incorrect syntax styles.
Hint: map + lambda applies function to each list item [OK]
Common Mistakes:
Thinking map needs a named function
Confusing square with doubling
Expecting original list unchanged
4. Find the error in this code using a lambda function:
add = lambda x, y:
return x + y
print(add(3, 4))
medium
A. Indentation error in lambda body
B. Lambda functions cannot have more than one parameter
C. Lambda functions cannot use return statement
D. Missing colon after lambda parameters
Solution
Step 1: Check lambda function syntax
Lambda functions are single expressions and cannot use statements like return.
Step 2: Identify the error in the code
The code incorrectly uses return inside a lambda, which is not allowed.
Final Answer:
Lambda functions cannot use return statement -> Option C
Quick Check:
Lambda = single expression, no return [OK]
Hint: Lambda is one expression, no return keyword [OK]
Common Mistakes:
Adding return inside lambda
Expecting multi-line lambda bodies
Confusing lambda with def function syntax
5. You want to sort a list of tuples data = [("apple", 2), ("banana", 1), ("cherry", 3)] by the second item in each tuple using a lambda function. Which code correctly does this?
hard
A. data.sort(lambda x: x[1])
B. data.sort(key=lambda x: x[1])
C. data.sort(key=lambda x: x[0])
D. data.sort(key=lambda y: y[2])
Solution
Step 1: Understand sorting with key parameter
The key argument takes a function to extract the sorting value from each item.
Step 2: Check lambda usage for sorting by second tuple item
Lambda should return the second item: x[1]. data.sort(key=lambda x: x[1]) uses correct syntax.
Final Answer:
data.sort(key=lambda x: x[1]) -> Option B
Quick Check:
Sort by second item = key=lambda x: x[1] [OK]
Hint: Use key=lambda x: x[index] to sort by tuple item [OK]