Daniel Gagnon
Daniel Gagnon

Reputation: 199

Complexity of searching a key in dictionary

What is the time complexity of this code:

 if 'key' in my_dict:
    print(my_dict['key'])

I just want to make sure the condition takes O(1). Is it right?

Upvotes: 0

Views: 603

Answers (1)

Ashwini Chaudhary
Ashwini Chaudhary

Reputation: 250891

From docs:

Operation Average Case Amortized Worst Case

Get Item  O(1)         O(n)

x in s    O(1)         O(n)  #From sets

Upvotes: 3

Related Questions