ProgrammingModerateTCS NQT · Accenture

What is printed?

What is printed?
print([y for x in [[1, 2], [3], []] for y in x if y % 2])
  1. A. [1, 2, 3]
  2. B. [1, 3]
  3. C. [2]
  4. D. [1, 3, 0]

Correct answer: B. [1, 3]

The first for clause walks the sublists, the second walks their items, and the filter keeps odd values. The empty sublist contributes nothing.

More Python Programming questions

Exams that ask this topic