ProgrammingHardAccenture · Cognizant · Python

What does the following Python 3 code print? import itertools print(list(itertools.compress([1, 2, 3, 4, 5], [1, 0, 1, 0, 1])))

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

Correct answer: C. [1, 3, 5]

compress keeps only the data elements whose matching selector is truthy: positions 0, 2, 4 -> 1, 3, 5.

More Python Programming questions

Exams that ask this topic