Complete the code to double each number in the list using map and a lambda function.
numbers = [1, 2, 3, 4] doubled = list(map(lambda x: x [1] 2, numbers)) print(doubled)
The lambda function multiplies each number by 2 using the * operator.
Complete the code to convert each string in the list to uppercase using map and a lambda.
words = ['apple', 'banana', 'cherry'] upper_words = list(map(lambda w: w[1](), words)) print(upper_words)
lower() will convert to lowercase instead.capitalize() only changes the first letter.The lambda calls the upper() method to convert each word to uppercase.
Fix the error in the lambda function to add 5 to each number using map.
nums = [10, 20, 30] result = list(map(lambda n: n [1] 5, nums)) print(result)
- subtracts 5 instead of adding.* multiplies instead of adding.The lambda should add 5 to each number using the + operator.
Fill both blanks to create a list of squares of even numbers only using map and filter with lambda.
numbers = [1, 2, 3, 4, 5, 6] even_squares = list(map(lambda x: x[1]2, filter(lambda x: x [2] 2 == 0, numbers))) print(even_squares)
+ or // instead of ** for squaring.// or + instead of % for checking evenness.The first lambda squares the number using **. The second lambda filters even numbers using the modulo operator %.
Fill all three blanks to create a dictionary with uppercase keys and values doubled only if value is greater than 10.
data = {'a': 5, 'b': 15, 'c': 20}
result = { [1]: [2]*2 for k, v in data.items() if v [3] 10 }
print(result)k instead of k.upper() for keys.v without doubling.< or == instead of > in the condition.The dictionary comprehension uses k.upper() for keys, doubles the value v, and filters values greater than 10 with >.