close

How to get the key from value in a dictionary in Python?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to get the key from value in a dictionary in Python in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to get the key from value in a dictionary in Python?

  1. How to get the key from value in a dictionary in Python?

    Basically, it separates the dictionary's values in a list, finds the position of the value you have, and gets the key at that position.

  2. get the key from value in a dictionary in Python

    Basically, it separates the dictionary's values in a list, finds the position of the value you have, and gets the key at that position.

Method 1

mydict = {'george': 16, 'amber': 19}
print mydict.keys()[mydict.values().index(16)]  # Prints george

Or in Python 3.x:

mydict = {'george': 16, 'amber': 19}
print(list(mydict.keys())[list(mydict.values()).index(16)])  # Prints george

Basically, it separates the dictionary’s values in a list, finds the position of the value you have, and gets the key at that position.

Method 2

There is none. dict is not intended to be used this way.

dictionary = {'george': 16, 'amber': 19}
search_age = input("Provide age")
for name, age in dictionary.items():  # for name, age in dictionary.iteritems():  (for Python 2.x)
    if age == search_age:
        print(name)

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read