• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Perform Reverse Dictionary Lookup in Python

Perform Reverse Dictionary Lookup in Python

June 3, 2022 Leave a Comment

There are a few ways you can do a reverse dictionary lookup in Python. The easiest way to perform a reverse dictionary lookup in Python is with a for loop.

d = {"name":"Bobby", "age":20,"height":65}

for key, value in d.items():
    if value == "Bobby":
        print(key)

#Output:
name

You can also invert the dictionary with dictionary comprehension and then access the key/value pair you want directly.

d = {"name":"Bobby", "age":20,"height":65}

d_inverted = {value: key for key, value in d.items()}

print(d_inverted["Bobby"])

#Output:
name

When working with dictionaries in Python, the ability to get information about the items of the dictionary is valuable.

One such situation where you need to do a little more work to access information about items in a dictionary is with a reverse dictionary lookup.

There are a few ways you can do a reverse dictionary lookup in Python. The easiest way to perform a reverse dictionary lookup in Python is with a for loop.

Below is an example showing you how to perform a reverse dictionary lookup using a for loop in your Python code.

d = {"name":"Bobby", "age":20,"height":65}

for key, value in d.items():
    if value == "Bobby":
        print(key)

#Output:
name

Inverting Dictionary to Perform Reverse Dictionary Lookup in Python

Another way you can perform reverse dictionary lookups is by inverting the dictionary you are working with and then getting the key you want directly via the value.

To invert a dictionary, you can use dictionary comprehension.

Then, after inverting your dictionary, just access the dictionary key/value pair with the value you are looking for.

Below is a simple example showing you how to invert a dictionary and perform a reverse lookup after inversion with Python.

d = {"name":"Bobby", "age":20,"height":65}

d_inverted = {value: key for key, value in d.items()}

print(d_inverted["Bobby"])

#Output:
name

Hopefully this article has been useful for you to learn how to perform reverse dictionary lookups in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Return Two Values from Function
  • 2.  Using Python to Append Character to String Variable
  • 3.  Check if Set Contains Element in Python
  • 4.  Python acosh – Find Hyperbolic Arccosine of Number Using math.acosh()
  • 5.  Sort Series in pandas with sort_values() Function
  • 6.  How to Write Pickle File to AWS S3 Bucket Using Python
  • 7.  Remove Substring from String in Python with replace()
  • 8.  Python max() function – Get Maximum of List or Dictionary with max() Function
  • 9.  Create List of Odd Numbers in Range with Python
  • 10.  How to Check if Number is Power of 2 in Python

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy