Python dict key lookup: Use in instead of d.keys()

Python dict key lookup: Use in instead of d.keys()

Want to check if a Python dict contains a key? Use “in” on the dict:

d = {'a':10, 'b':20, 'c':30}

‘a’ in d # True
‘x’ in d # False

Searching in d.keys() is far slower:

‘a’ in d # 10 ns
‘a’ in d.keys() # 29 ns (!)