ProgrammingModerateTCS NQT · Accenture

For a Python dict d with n entries, which operation costs O(n) rather than O(1) on average?

  1. A. list(d.keys())
  2. B. k in d
  3. C. d.get(k)
  4. D. d[k] = v

Correct answer: A. list(d.keys())

Membership, lookup and assignment use hashing and cost O(1) on average. Building a list of the keys must copy all n keys, which costs O(n).

More Python Programming questions

Exams that ask this topic