How do you check the presence of many keys in a Python dictinary?

Posted by Thierry Lam on Stack Overflow See other posts from Stack Overflow or by Thierry Lam
Published on 2010-05-11T19:24:45Z Indexed on 2010/05/11 19:34 UTC
Read the original article Hit count: 191

Filed under:

I have the following dictionary:

sites = {
    'stackoverflow': 1,
    'superuser': 2,
    'meta': 3,
    'serverfault': 4,
    'mathoverflow': 5
}

To check if there are more than one key available in the above dictionary, I will do something like:

'stackoverflow' in sites and 'serverfault' in sites

The above is maintainable with only 2 key lookups. Is there a better way to handle checking a large number of keys in a very big dictionary?

© Stack Overflow or respective owner

Related posts about python