ProgrammingHardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
from itertools import chain
print(list(chain.from_iterable(['ab', 'c'])), ''.join(chain(*[['x'], ['y', 'z']])))
  1. A. ['a', 'b', 'c'] xyz
  2. B. ['ab', 'c'] xyz
  3. C. ['a', 'b', 'c'] x y z
  4. D. ['ab', 'c'] xy

Correct answer: A. ['a', 'b', 'c'] xyz

chain.from_iterable flattens one level, and a string is iterated character by character, so the result is ['a', 'b', 'c']. chain(*...) flattens the nested lists, which join to xyz.

More Python Programming questions

Exams that ask this topic