Obtaining a value when given a key in python dicionaries

0 votes
Can we obtain the key from a dictionary when a value is provided?
May 31, 2018 in Python by NEET

recategorized Jun 1, 2018 by Sudhir 185 views

2 answers to this question.

0 votes

We cannot. Dictionaries aren't meant to be used that way. You can iterate through the dictionary using iteritems() instead - 

for name, id in list.iteritems():
    if id == search_id:
        print name
answered May 31, 2018 by Nietzsche's daemon
• 4,260 points
0 votes

Yes you can check below code

dictionary = {'george' : 16, 'amber' : 19}
search_age = int(input("Provide age"))
search_name =input("enter name")
for key, value in dictionary.items():
    if value == search_age:
        print(key, '::', value)
    if key == search_name:
        print(key, '::', value)

Refer this link for more dictionary operations


answered 3 days ago by Suhas

Related Questions In Python

0 votes
1 answer

I have a dictonary in python how to access the value field?

dic={"car":["limo","sedan"]} print (dic.keys())    <-----------------------Fetch the key "car" print (dic['car'][0])   <------------------------Fetch ...READ MORE

answered Dec 20, 2018 in Python by Shuvodip
211 views
0 votes
1 answer
0 votes
1 answer

Why am I getting a error when I am printing two different data types in python?

different data type is being used. that ...READ MORE

answered Mar 6, 2019 in Python by Mohammad
• 3,230 points
246 views
0 votes
1 answer

can we sort the key value pairs in a dictionary?

we can use OrderedDict import collections result = colections.Ord ...READ MORE

answered Mar 22, 2019 in Python by Mohammad
• 3,230 points
155 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 2,473 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,810 points
1,854 views
+4 votes
7 answers

Splitting a List into chunks in Python

Here's a generator that yields the chunks ...READ MORE

answered Apr 13, 2018 in Python by Nietzsche's daemon
• 4,260 points
32,963 views
+1 vote
8 answers

Count the frequency of an item in a python list

To count the number of appearances: from collections ...READ MORE

answered Oct 18, 2018 in Python by tinitales
26,157 views