What is the time complexity of looking up a value by its key in a Python dictionary under average operational conditions?

Answer and explanation

Correct answer: A. O(1)

  1. Step 1: Recall that Python dictionaries are implemented using dynamic hash tables.
  2. Step 2: With an effective hash function and sparse hash collisions, computing the hash and accessing the bucket index executes in constant average time.
Answer
O(1).
Why not the others: is worst-case when all keys collide into the same bucket; is tree-based map lookup; is polynomial.

Practise more Algorithms and Python Programming.

Free questions with an explanation for every one, and full timed EmSAT Achieve (UAE) mock tests.