QuestionQ103

Miscellaneous (List Comprehensions, Lambdas, Closures, I/O)

What output is produced by the following code?

myli = [1, 2, 4]  
m = list(map(lambda x: 2**x, myli))  
print(m[-1])  
  • A 4
  • B 1
  • C an exception is raised
  • D 16
Explanation

Applying lambda x: 2**x to [1, 2, 4] creates [2, 4, 16]. The index -1 accesses the final element, which is 16.

Community Discussion

No comments yet. Be the first to start the discussion!