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])))- A. [1, 2, [3, 4], 6]
- B. [1, 2, 3, 4]
- C. [1, 2, 3, 4, 5]
- 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
- What is printed? python print('42'.zfill(5), '-42'.zfill(5), 'ab'.center(6, ''), 'x'.ljust(3, '.') + '|')
- What is printed? python print(10 / 2, 10 // 2, 7 / 7, 2 3 + 4)
- What is printed? python print('abc'.index('c'), 'abcabc'.rfind('b'), 'abc'.find('d'), len('abc'[1:]))
- What is printed? python print(sorted('bca'), ''.join(sorted('bca', reverse=True)), sorted([3, 1, 2], reverse=True)[0], list(reversed('ab')))
- What is printed? python print(int('12') + int('3'), '12' + '3', 12 + 3 == int('15'), 'x')
- What is printed? python s = 'Python' print(s[0], s[-1], s[1:4], s[::2])