ProgrammingHardTCS NQT · Infosys pattern

What is printed?

What is printed?
nums = [1, 2, 3]
doubled = list(map(lambda x: x * 2, nums))
evens = list(filter(lambda x: x % 2 == 0, nums))
print(doubled, evens)
  1. A. [2, 4, 6] [1, 3]
  2. B. [1, 2, 3] [2]
  3. C. [2, 4, 6] [2]
  4. D. [2, 4, 6] [1, 2, 3]

Correct answer: C. [2, 4, 6] [2]

map applies the doubling lambda to every element, giving [2,4,6]. filter keeps only elements where the lambda returns truthy (even numbers), keeping just 2 from the original list.

More Python Programming questions

Exams that ask this topic