What's the non brute force way to filter a Python dictionary?

Posted by Thierry Lam on Stack Overflow See other posts from Stack Overflow or by Thierry Lam
Published on 2010-04-27T21:23:56Z Indexed on 2010/04/27 21:33 UTC
Read the original article Hit count: 202

Filed under:

I can filter the following dictionary like:

data = {
    1: {'name': 'stackoverflow', 'traffic': 'high'},
    2: {'name': 'serverfault', 'traffic': 'low'},
    3: {'name': 'superuser', 'traffic': 'low'},
    4: {'name': 'mathoverflow', 'traffic': 'low'},
}

traffic = 'low'

for k, v in data.items():
    if v['traffic'] == traffic:
        print k, v

Is there an alternate way to do the above filtering?

© Stack Overflow or respective owner

Related posts about python