simple question about dictionaries

S

skazhy

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!
 
J

Jeff

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))
 
F

Fredrik Lundh

skazhy said:
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>
 
B

Bruno Desthuilliers

skazhy a écrit :
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?

What if many keys are associated with a same value, ie:
d = {'a':100, 'b':200, 'c':100}

somehting like this:
dict = { "key1"=100,"key2"=200,"key3"=300}

1/ don't use 'dict' as an indentifier - this shadows the builtin dict type.

2/ the 'dict literal' syntax is:

d = {'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..

Assuming you don't have duplicate values, you just need to build a
reverse dictionnary, that is using values as keys and keys as values,
then lookup this dictionnary. The first operation can be done in one
single line.

reversed_d = dict((value, key) for key, value in d.items())
 

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

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top