Valdas Valdas - 28 days ago 8
Python Question

Check value in dictionary of dictionaries

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

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


I want to change
item in list.values()
so that it only compares
url
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

Use any and a generator expression:

if any(i['url'] == item['url'] for i in dict.values()):
    print 'yes'
else:
    print 'no'