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?
At some level the filter will have to do exactly what you describe. If you’re going to filter on the values, you’ll have to process each one, one-by-one.