Tuesday, December 15, 2015

serch key in python dictionary using 'in' in place of has_key or .keys()

best way to use 'in'

a = {'k': 'x', 'p': 'k'}

if k in a:
     print a['k']

k in a => looks 'k' only in the key list.'k' in  a.keys() is not needed - This will unnecessarily slow the processing speed.

No comments:

Post a Comment