Python Lambda Expression
Python Lambda Expression
Lambda expressions, also known as lambda functions, are a concise way to create
small anonymous functions in Python. They allow you to define functions inline,
without the need for a formal function definition. Lambda functions are particularly
useful when you need a simple function for a short period and don't want to go
through the process of defining a separate named function. Here's a detailed
breakdown of lambda expressions in Python:
1.
Syntax: The syntax of a lambda function is quite simple. It consists of the lambda
keyword followed by one or more arguments, a colon :, and then the expression to be
evaluated.
2.
3.
python
4.
5.
Copy code
6.
7.
lambda arguments: expression
8.
9.
10.
Anonymous Functions: Lambda functions are anonymous, meaning they don't have
a name. They are created at runtime and can be used immediately.
11.
12.
Single Expression: Lambda functions are restricted to a single expression. This
expression is evaluated and returned when the lambda function is called.
13.
14.
Usage: Lambda functions are often used as arguments to higher-order functions
(functions that take other functions as arguments), such as map(), filter(), and
sorted().
15.
16.
python
17.
18.
Copy code
19.
20.
# Example: Using lambda function with map()
numbers = [1, 2, 3, 4, 5]
squared = list(map(lambda x: x**2, numbers))# Output: [1, 4, 9, 16, 25]
21.
22.
23.
Avoiding Defining a Separate Function: Lambda expressions are particularly
handy when the functionality of the function is quite simple and you don't want to
define a separate named function.
24.
25.
python
26.
27.
Copy code
28.
29.
# Example: Sorting a list of tuples by the second element
pairs = [(1, 'one'), (2, 'two'), (3, 'three'), (4, 'four')]
sorted_pairs = sorted(pairs, key=lambda pair: pair[1])# Output: [(4,
'four'), (1, 'one'), (3, 'three'), (2, 'two')]
30.
31.
32.
Limitations: While lambda functions can be useful, they are limited in their
capabilities compared to named functions. They are meant for simple operations and
can't contain statements like return, pass, or even multi-line expressions.
33.
34.
Readability vs. Conciseness: While lambda functions can make code more concise,
they can also make it less readable if overused or used inappropriately. It's important
to strike a balance between conciseness and readability.
35.
MCQs
**Answer: B) lambda**
**Answer: A) 10**
**Answer: A) map()**
16. Lambda functions are commonly used with which built-in function to sort iterables?
- A) sort()
- B) sorted()
- C) sort_by()
- D) order()
**Answer: B) sorted()**
17. Which of the following is a correct way to pass a lambda function as an argument?
- A) `map(my_lambda_function, my_list)`
- B) `map(my_list, my_lambda_function)`
- C) `map(lambda my_lambda_function, my_list)`
- D) `map(lambda x: my_lambda_function(x), my_list)`
**Answer: B) False**
21. Lambda functions are often used with which built-in function to filter iterables based on a
specified condition?
- A) filter()
- B) select()
- C) extract()
- D) sift()
**Answer: A) filter()**
22. Which of the following is a correct way to define a lambda function with multiple arguments?
- A) `lambda x, y: x * y`
- B) `lambda (x, y): x * y`
- C) `lambda x y: x * y`
- D) `lambda x y -> x * y`
**Answer: A) 7**
24. Lambda functions can be used to create dictionaries using which built-in function?
- A) dict()
- B) map()
- C) lambda_dict()
- D) convert()
**Answer: A) dict()**
25. Which of the following is a correct way to sort a list of tuples based on the second element using a
lambda function?
- A) `sorted(my_list, key=lambda x: x[1])`
- B) `sort_by(my_list, lambda x: x[1])`
- C) `my_list.sort(key=lambda x: x[1])`
- D) `sort(my_list, lambda x: x[1])`
**Answer: A) Arguments**
**Answer: A) 1**
31. Which function is used to apply a lambda function to each element of a list and return a new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()
**Answer: A) map()**
**Answer: A) 16**
34. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`
**Answer: D) 9**
38. Which built-in function is used to apply a lambda function to each element of an iterable and
return elements for which the function returns True?
- A) map()
- B) reduce()
- C) filter()
- D) apply()
**Answer: C) filter()**
**Answer: A) "nohtyP"**
41. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable?
- A) max()
- B) maximum()
- C) largest()
- D) peak()
**Answer: A) max()**
42. Which of the following is true regarding lambda functions in Python?
- A) They are only used with named functions
- B) They can't accept arguments
- C) They are more memory-efficient than named functions
- D) They are less flexible than named functions
**Answer: B) False**
**Answer: C) Lists**
45. Which built-in function is used to apply a lambda function to each element of an iterable and
return the result as a list?
- A) list_apply()
- B) lambda_apply()
- C) map()
- D) apply()
**Answer: C) map()**
**Answer: A) "even"**
**Answer: B) Strings**
51. Which of the following is a correct way to pass a lambda function as an argument to the sorted()
function?
- A) `sorted(my_list, my_lambda_function)`
- B) `sorted(my_lambda_function, my_list)`
- C) `sorted(my_list, key=my_lambda_function)`
- D) `sorted(key=my_lambda_function, my_list)`
**Answer: A) Variables**
**Answer: D) "ello"**
57. Lambda functions are commonly used with which built-in function to calculate the minimum value
in an iterable?
- A) min()
- B) minimum()
- C) smallest()
- D) low()
**Answer: A) min()**
58. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`
59. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()
**Answer: A) sum()**
60. What is the output of the following code snippet?
```python
my_lambda = lambda x: x % 2 == 0
print(my_lambda(6))
```
- A) True
- B) False
- C) Error
- D) None
**Answer: A) True**
**Answer: A) 1**
65. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()
**Answer: A) map()**
66. What is the output of the following code snippet?
```python
my_lambda = lambda x: x ** 2 if x % 2 == 0 else x
print(my_lambda(4))
```
- A) 16
- B) 8
- C) 4
- D) 2
**Answer: A) 16**
68. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`
**Answer: A) Arguments**
**Answer: A) "nohtyP"**
73. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable?
- A) max()
- B) maximum()
- C) largest()
- D) peak()
**Answer: A) max()**
74. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`
75. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()
**Answer: A) sum()**
**Answer: A) True**
**Answer: A) 1**
80. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()
**Answer: A) map()**
**Answer: A) 16**
83. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`
**Answer: A) Arguments**
**Answer: A) "nohtyP"**
88. Lambda functions can be used with which built-in function to calculate the maximum value in an
iterable
?
- A) max()
- B) maximum()
- C) largest()
- D) peak()
**Answer: A) max()**
89. Which of the following is a correct way to use a lambda function with the reduce() function?
- A) `reduce(lambda x, y: x + y, my_list)`
- B) `reduce(my_list, lambda x, y: x + y)`
- C) `reduce(lambda x, y: x + y)`
- D) `reduce(my_list, lambda x, y: x + y, 0)`
90. Lambda functions can be used with which built-in function to calculate the sum of elements in an
iterable?
- A) sum()
- B) add()
- C) total()
- D) accumulate()
**Answer: A) sum()**
**Answer: A) True**
**Answer: A) 1**
95. Which built-in function is used to apply a lambda function to each element of a list and return a
new list?
- A) map()
- B) apply()
- C) transform()
- D) filter()
**Answer: A) map()**
**Answer: A) 16**
98. Which of the following is a correct way to define a lambda function that returns a tuple?
- A) `lambda x, y: (x, y)`
- B) `lambda (x, y): (x, y)`
- C) `lambda x y: (x, y)`
- D) `lambda x y -> (x, y)`
**Answer: A) Arguments**