looking up dictionaries by values

S

Sandman

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.
 
S

Steve Holden

Sandman said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,222
Messages
2,571,140
Members
47,755
Latest member
Grazynkaa

Latest Threads

Top