Python Programming - Lambda Functions

Exercise : Lambda Functions - General Questions
  • Lambda Functions - General Questions
56.
What is the output of the following code?
items = ['apple', 'banana', 'cherry']
filtered = list(filter(lambda x: 'a' in x, items))
print(filtered)
['apple', 'banana', 'cherry']
['apple', 'banana']
['apple']
['banana']
Answer: Option
Explanation:
The filter() function with the lambda filters items containing the letter 'a'.

57.
How can a lambda function be used with the min() function to find the minimum length string in a list of strings?
min(strings, key=len)
min(strings, lambda x: x)
min(strings, lambda x: len(x))
min(strings, key=lambda x: x)
Answer: Option
Explanation:
The key parameter is used to specify the lambda function that calculates the length of each string.

58.
What is the purpose of the reduce() function when used with a lambda function?
To filter elements from an iterable based on the lambda function's condition.
To create a sorted list based on the lambda function's values.
To apply the lambda function cumulatively to the items of an iterable.
To combine two iterables into a single iterable of tuples.
Answer: Option
Explanation:
The reduce() function is used to apply the lambda function cumulatively to the items of an iterable.

59.
What does the following lambda function do?
lambda x, y: x + y
Concatenates two strings.
Multiplies two numbers.
Adds two numbers.
Raises one number to the power of another.
Answer: Option
Explanation:
The lambda function adds two numbers.

60.
When using the sorted() function with a lambda function to sort a list of tuples, how can you sort based on the second element of each tuple?
sorted(tuples, key=lambda x: x[1])
sorted(tuples, sort_key=lambda x: x[1])
sorted(tuples, key=tuple[1])
sorted(tuples, sort_key=tuple[1])
Answer: Option
Explanation:
The key parameter is used to specify the lambda function that extracts the second element for sorting.