• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Dictionary / Get key from value in dictionary

Get key from value in dictionary

Author: Aditya Raj
Last Updated: April 23, 2021

In python, we can get the values present in a dictionary using the keys by simply using the syntax dict_name[key_name]. But there isn’t any method to extract a key associated with the value when we have values. In this article, we will see the ways with help of which we can get the key from a given value in a dictionary.

Get key from a value by searching the items in a dictionary

This is the simplest way to get a key for a value. In this method we will check each key-value pair to find the key associated with the present value.For this task, we will use the items() method for a python dictionary. The items() method returns a list of tuples containing key value pairs. We will search each tuple to find the key associated with our value as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_items=myDict.items()
print("Given value is:")
myValue="PFB"
print(myValue)
print("Associated Key is:")
for key,value in dict_items:
    if value==myValue:
        print(key)

Output:

Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated Key is:
acronym

In the above program, we have  created a list of items in myDict using myDict.items() and then we check for each item in the list to find the key for our value. 

Get key from a value by using python lists

We can create a separate list of keys and values and then we can retrieve the keys from the given value using index() method. For this task, we will first create a list of keys present in the dictionary using keys() method and then we will create the list of values present in the dictionary using values() method. Now we will get the index of the given value from the list of values using index() method. As we know that the list of keys has the same order of keys as values in the list of values, the index of the values in the list of values will be the same as the index of the associated key in the list of keys. So, after finding the index of the value in the list of values, we can find the key in the list of keys at the same index. This can be done as follows.

myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_keys=list(myDict.keys())
dict_values=list(myDict.values())
print("Given value is:")
myValue="PFB"
print(myValue)
val_index=dict_values.index(myValue)
print("Associated key is:")
myKey=dict_keys[val_index]
print(myKey)

Output:

Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated key is:
acronym

Get key from a value by using list comprehension

Instead of using index() method, we can use list comprehension to get the keys associated with the given value. To find the key, we will create a list of keys which have associated values equal to the given value using list comprehension as follows.


myDict={"name":"PythonForBeginners","acronym":"PFB"}
print("Dictionary is:")
print(myDict)
dict_items=myDict.items()
print("Given value is:")
myValue="PFB"
print(myValue)
print("Associated key is:")
myKey=[key for key,value in dict_items if value==myValue]
print(myKey)

Output:

Dictionary is:
{'name': 'PythonForBeginners', 'acronym': 'PFB'}
Given value is:
PFB
Associated key is:
['acronym']

Conclusion

In this article, we have seen three ways to obtain a key from a python dictionary using list comprehension, items() method and list index() method. Stay tuned for more informative articles.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Dictionary Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup bitly Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehensions in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Pandas Append Row to DataFrame
  • Convert String to DataFrame in Python
  • Pandas DataFrame to List in Python
  • Solved: Dataframe Constructor Not Properly Called Error in Pandas
  • Overwrite a File in Python

Copyright © 2012–2023 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us