1.

What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))?(a) [1|x for x in [1, 2, 3]](b) [-1**x for x in [1, 2, 3]](c) [x**-1 for x in [1, 2, 3]](d) [x^-1 for x in range(4)]This question was posed to me during a job interview.I'd like to ask this question from List Comprehension topic in chapter Lists & List Comprehension of Python

Answer»

Right answer is (c) [x**-1 for x in [1, 2, 3]]

To explain I would say: The output of the FUNCTION list(map(lambda x:x**-1, [1, 2, 3])) is [1.0, 0.5, 0.3333333333333333] and that of the list comprehension [x**-1 for x in [1, 2, 3]] is [1.0, 0.5, 0.3333333333333333]. HENCE the answer is: [x**-1 for x in [1, 2, 3]].



Discussion

No Comment Found

Related InterviewSolutions