my_list = [1, 2, 3] # Insert line of code here. print(foo)
Which snippet would you insert in order for the program to output the following result (tuple):
(1, 4, 27)
- foo = list(map(lambda x: x*x, my_list))
- foo = tuple(map(lambda x: x**x, my_list))
- foo = tuple(map(lambda x: x*x, my_list))
- foo = list(map(lambda x: x**x, my_list))
Explanation: In order to obtain the tuple mentioned, a map function is used. It takes the lambda function lambda x: x**x and the list my_list as arguments. It performs the exponentiation operation x**x defined within the lambda function with each of the lists elements [1, 2, 3]. The result is cast to a tuple and printed in the console.
More Questions: Python Essentials 2 – Module 4 Test
Please login or Register to submit your answer