By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,048 Members | 1,066 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,048 IT Pros & Developers. It's quick & easy.

looking up dictionaries by values

P: n/a
Hi there,
I'm still a bit new to Python, and had a question.
I have a dictionary that looks like:
PWD = \
{
"root": 0,
"joe": 200,
"susan": 201,
...
}

In other words, the values are unique as well as the keys. I've run
into a situation where I need to lookup the item by value. So given
201, I need to say the key is really susan.

Is there a way to do this that does not traverse the entire dictionary
multiple times?

Thanks.

Jul 18 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
See the thread from earlier today, this list (py*********@python.org).

"newbie: dictionary - howto get key value"

On Thursday 10 March 2005 06:26 pm, Sandman wrote:
Hi there,
I'm still a bit new to Python, and had a question.
I have a dictionary that looks like:
PWD = \
{
"root": 0,
"joe": 200,
"susan": 201,
..
}

In other words, the values are unique as well as the keys. I've run
into a situation where I need to lookup the item by value. So given
201, I need to say the key is really susan.

Is there a way to do this that does not traverse the entire dictionary
multiple times?

Thanks.


--
James Stroud, Ph.D.
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
Jul 18 '05 #2

P: n/a
Sandman wrote:
Hi there,
I'm still a bit new to Python, and had a question.
I have a dictionary that looks like:
PWD = \
{
"root": 0,
"joe": 200,
"susan": 201,
..
}

In other words, the values are unique as well as the keys. I've run
into a situation where I need to lookup the item by value. So given
201, I need to say the key is really susan.

Is there a way to do this that does not traverse the entire dictionary
multiple times?

Thanks.

One way would be to keep two parallel dictionaries so you have both
mappings. If necessary you could wrap this up as an object to make it
easier to deal with.

regards
Steve

Jul 18 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.