ProgrammingModerateInfosys HackWithInfy · TCS CodeVita

What is printed?

What is printed?
def flat(x):
    for i in x:
        if isinstance(i, list):
            yield from flat(i)
        else:
            yield i
print(list(flat([1, [2, [3, 4]], [], 5])))
  1. A. [1, 2, [3, 4], 6]
  2. B. [1, 2, 3, 4]
  3. C. [1, 2, 3, 4, 5]
  4. D. [1, [2, [3, 4]], 7]

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

The generator recurses into each nested list with yield from and yields plain values. The empty list contributes nothing.

More Python Programming questions

Exams that ask this topic