Search a Python Dictionary Both Ways
By
Ene Uran on
Jan 20th, 2008 1:21 am Most commonly a dictionary of key:value pairs is searched by the unique key. Sometimes it becomes necessary to search for the key, or keys given a value. Special consideration has to be given to this case, because the value does not have to be unique and may return several keys (list of keys).
A class inheriting the dictionary base class is used to do both searches. I have also made up some simple, but nicely named functions to search the dictionary, in case you don't want to use a class.
Much thanks! Your code is very helpful (and efficient too!) :)
However, in defining "find_key", shouldn't you use the argument "dic" instead of "symbol_dic" in calling iteritems? :)
Other than that, thanks for the big help! :)
teci
Newbie Poster
2 posts since Jun 2008
Reputation Points: 10
Solved Threads: 1
Skill Endorsements: 0
© 2013 DaniWeb® LLC
Page rendered in 0.0557 seconds
using 2.64MB