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

simple question about dictionaries

P: n/a
hi, i am new to python, so i've a really simple question about
dictionaries.
if i have a dictionary and I make have an input after it (to input
numbers) can i get the key of value that was in input?

somehting like this:
dict = { "key1"=100,"key2"=200,"key3"=300}
a = input()
print 'the key of inputted value is', dict['a']

this syntax of course is wrong, but i hope you got the point..

thanks in advance!

Jul 21 '08 #1
Share this Question
Share on Google+
6 Replies

P: n/a
Sounds like a school assignment. Find the answer yourself here:
http://diveintopython.org/toc/index.html

You'll learn a lot more in the process.

2B
Jul 21 '08 #2

P: n/a
On Jul 21, 7:35*am, skazhy <ska...@gmail.comwrote:
hi, i am new to python, so i've a really simple question about
dictionaries.
if i have a dictionary and I make have an input after it (to input
numbers) can i get the key of value that was in input?

somehting like this:
dict = { "key1"=100,"key2"=200,"key3"=300}
a = input()
print 'the key of inputted value is', dict['a']

this syntax of course is wrong, but i hope you got the point..

thanks in advance!
I don't think there is a built-in that retrieves dict keys by value,
but if the dictionary were small enough, you could search the list of
key/value pairs returned by dict.items(). You can also iterate
through all pairs with dict.iteritems(), which returns an iterator.
Something like:

def key_by_value(dct, val):
for k, v in dct.iteritems():
if v == val:
return v
throw KeyError('%s not found' % str(val))

Jul 21 '08 #3

P: n/a
skazhy wrote:
hi, i am new to python, so i've a really simple question about
dictionaries.
if i have a dictionary and I make have an input after it (to input
numbers) can i get the key of value that was in input?
A dictionary contains (key, value) pairs, and is optimized for quickly
finding the value in a pair, if given the key.
somehting like this:
dict = { "key1"=100,"key2"=200,"key3"=300}
dict() is a built-in function; you're free to reuse the names of such
functions as variable names in Python, but if you do, you won't be able
to use those functions in that part of your program. And in this case,
the dict() built-in might be useful (see below):
a = input()
print 'the key of inputted value is', dict['a']

this syntax of course is wrong, but i hope you got the point..
Assuming that you want the *key* in Python's sense, for a given value,
you can either loop over the dictionary (called D below):

for k, v in D.items():
if v == a:
print 'the key of inputted value is', v
break
else:
print "not found"

or create an reverse mapping and use that (you only have to do this
every time the dictionary changes, of course), e.g.

reverse_D = dict((D[k], k) for k in D)

a = input()
print "the key for value", a, "is", reverse_D[a]

(this doesn't work if you call your dictionary "dict", obviously.)

</F>

Jul 21 '08 #4

P: n/a
Jeff wrote:
throw KeyError('%s not found' % str(val))
"throw"? and shouldn't that be a ValueError? ;-)

</F>

Jul 21 '08 #5

P: n/a
On Jul 21, 8:14*am, Fredrik Lundh <fred...@pythonware.comwrote:
Jeff wrote:
* throw KeyError('%s not found' % str(val))

"throw"? *and shouldn't that be a ValueError? ;-)

</F>
Whoops. Been working in too many different languages at the same
time :).
Jul 22 '08 #6

P: n/a
On Jul 21, 8:14*am, Fredrik Lundh <fred...@pythonware.comwrote:
Jeff wrote:
* throw KeyError('%s not found' % str(val))

"throw"? *and shouldn't that be a ValueError? ;-)

</F>
Whoops. Been working in too many different languages at the same
time :).
Jul 22 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.