ProgrammingHardProduct Company SDE · Python internals

What does the following Python 3 code print? print([i for i in range(5) if i != 2][::-1])

  1. A. [0, 1, 3, 4]
  2. B. [0, 1, 2, 3, 4]
  3. C. [4, 3, 2, 1, 0]
  4. D. [4, 3, 1, 0]

Correct answer: D. [4, 3, 1, 0]

The comprehension first builds [0,1,3,4] (skipping 2), then [::-1] reverses that filtered list.

More Python Programming questions

Exams that ask this topic