ProgrammingModerateTCS NQT · Accenture

For a Python list a, which operation takes time proportional to j - i?

  1. A. a[i]
  2. B. len(a)
  3. C. a.append(x)
  4. D. a[i:j]

Correct answer: D. a[i:j]

Slicing copies the selected references into a new list, so its cost grows with the slice length j - i. Indexing, len and append do not depend on the slice size.

More Python Programming questions

Exams that ask this topic