ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
nums = map(int, '1 2 3'.split())
a = list(nums)
b = list(nums)
print(a, b)
  1. A. [1, 2, 3] [None, None, None]
  2. B. [1, 2, 3] []
  3. C. [1, 2, 3] [1, 2, 3]
  4. D. [] []

Correct answer: B. [1, 2, 3] []

map returns a one-shot iterator. The first list() consumes it, so the second list() gets nothing. Keep the result as a list if you need to traverse it twice.

More Python Programming questions

Exams that ask this topic