473,398 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

looking up dictionaries by values

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
2 1236
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Frohnhofer, James | last post by:
My initial problem was to initialize a bunch of dictionaries at the start of a function. I did not want to do def fn(): a = {} b = {} c = {} . . . z = {}
3
by: Shivram U | last post by:
Hi, I want to store dictionaries on disk. I had a look at a few modules like bsddb, shelve etc. However would it be possible for me to do the following hash = where the key is an int and not...
22
by: Martin MOKREJ© | last post by:
Hi, I'm looking for some easy way to do something like include in c or PHP. Imagine I would like to have: cat somefile.py a = 222 b = 111 c = 9
210
by: Christoph Zwerschke | last post by:
This is probably a FAQ, but I dare to ask it nevertheless since I haven't found a satisfying answer yet: Why isn't there an "ordered dictionary" class at least in the standard list? Time and again...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
16
by: IamIan | last post by:
Hello, I'm writing a simple FTP log parser that sums file sizes as it runs. I have a yearTotals dictionary with year keys and the monthTotals dictionary as its values. The monthTotals dictionary...
4
by: kdt | last post by:
Hi Trying to create a function that takes two dictionaries, and deletes key:values that are common in both dictionaries. So far I have the following; but I can only delete values in one dictionary...
9
by: Brandon | last post by:
Hi all, I am not altogether experienced in Python, but I haven't been able to find a good example of the syntax that I'm looking for in any tutorial that I've seen. Hope somebody can point me...
14
by: cnb | last post by:
Are dictionaries the same as hashtables?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.