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 826 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)
answered Nov 25, 2021 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
796 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
1,040 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
570 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 4,434 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,790 points
4,070 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
35,402 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
35,999 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP