Livin wrote:
> I'm a noobie so please be easy on me. I have searched a ton and did not find
> anything I could understand.
>
> I'm using py2.3
>
> I've been using Try/Except but this gets long with multiple dictionaries.
>
> I found some code on web pages and such but cannot get them to work. Any
> help is appreciated as I need to search multiple dictionaries for keys.
>
>
>
> here's the code I found but cannot get to work...
>
> dict_set = (self.dictHSdevices, self.dictHSevents, self.dictHSbtnDevices)
> <-- /creates set of dictionaries/
>
> /code to search the set/-->
>
> val = [ d for d in dict_set if d[value] in dict_set ]
> OR
> for key, value in dict.iteritems(): pass
If I understood correctly what you want to do, here's a one-liner that
does it:
def lookup(value, *dicts):
return [d.get(value) for d in dicts if value in d]
>>> a = {'x':1, 'y':2}
>>> b = {'y':4, 'z':7}
>>> lookup('y', a, b)
[2, 4]
It's not the *most* efficient way because value is looked up twice if
it is contained in the dictionary; if you absolutely need it to be as
efficient as possible and can't figure it out on your own, ask again
and someone will help you out.
George
--
http://mail.python.org/mailman/listinfo/python-list