Valdas Valdas - 1 year ago 98
Python Question

Check value in dictionary of dictionaries

foob = {1:{'url':'', 'name':'abc'}, 2:{'url':'', 'name':'def'}}
item = {'url':'', 'name':'wrong name'}

if item in foob.values():
print 'yes'
print 'no'

I want to change
item in list.values()
so that it only compares
values and not whole dictionary. Is it possible to do that without iterating through whole dictionary there a simple way to do this without writing a separate for loop?

Answer Source

Use any and a generator expression:

if any(i['url'] == item['url'] for i in dict.values()):
    print 'yes'
    print 'no'
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download