Little experiment:
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "d.has_key('a')"
1000000 loops, best of 100: 0.361 usec per loop
python -m timeit -r 100 -s "d={'a':'1', 'b':'2', 'c':'3'}" "'a' in d"
10000000 loops, best of 100: 0.159 usec per loop
Checking if dictionary has key using “in” is faster than using has_key() method.
Tested in Python 2.6
0 comments ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment