ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
lst = list(range(5))
lst[1:4] = []
try:
    lst[::2] = [9, 9]
except ValueError:
    print('ValueError', lst)
  1. A. ValueError [0, 9]
  2. B. ValueError [0, 4]
  3. C. ValueError [9, 9]
  4. D. [9, 9]

Correct answer: B. ValueError [0, 4]

After removing indices 1 to 3 the list is [0, 4]. Its extended slice [::2] has only one position, index 0, so assigning two values raises ValueError, and the list is left unchanged.

More Python Programming questions

Exams that ask this topic