ProgrammingModerateTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
import itertools
print(list(itertools.zip_longest('ab', 'xyz', fillvalue='-')))
  1. A. [('a', 'x'), ('b', 'y'), ('c', 'z')]
  2. B. [('a', 'x'), ('b', 'y'), ('-', 'z')]
  3. C. [('a', 'x'), ('b', 'y')]
  4. D. [('a', 'x'), ('b', 'y'), ('z', 'z'), ('-', '-')]

Correct answer: B. [('a', 'x'), ('b', 'y'), ('-', 'z')]

zip_longest keeps going until the longest input is exhausted, filling the shorter one with fillvalue. zip alone would stop after two pairs.

More Python Programming questions

Exams that ask this topic