• 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 / Inverting Dictionary Variables in Python with Dictionary Comprehension

Inverting Dictionary Variables in Python with Dictionary Comprehension

June 6, 2022 Leave a Comment

To invert a dictionary and swap the keys and values of a dictionary in Python, use dictionary comprehension and swap the keys and values.

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

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

print(d_inverted)

#Output:
{'Bobby': 'name', 20: 'age', 65: 'height'}

When working with dictionaries in Python, the ability to modify your variables to get new values easily is valuable.

One such situation is if you want to invert a dictionary variable. Inverting a dictionary means swapping the keys and values so the values with become the new keys and the keys will become the new values.

To invert a dictionary, you can use dictionary comprehension.

Below is a simple example showing you how to invert a dictionary with Python.

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

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

print(d_inverted)

#Output:
{'Bobby': 'name', 20: 'age', 65: 'height'}

Performing Reverse Dictionary Lookup After Inverting Dictionary in Python

One case where inverting a dictionary might be useful is if you want to perform a reverse dictionary lookup, i.e. you want to find a key based on a specific value.

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

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

Inverting Dictionary to Remove Duplicate Values from Dictionary in Python

One other use where inverting a dictionary might be useful is if you want to remove duplicate values from a dictionary.

Since dictionaries can only have a unique set of keys, when you go to invert it, you will remove any duplicate values.

Below is an example of inverting a dictionary with duplicate values. You can see that only one key is kept for each value.

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

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

#Output:
{'Bobby': 'name', 20: 'credits', 65: 'income'}

Hopefully this article has been useful for you to learn how to invert dictionary variables in Python.

Other Articles You'll Also Like:

  • 1.  Check if Variable is Datetime in Python
  • 2.  pandas min – Find Minimum Value of Series or DataFrame
  • 3.  Convert False to 0 in Python
  • 4.  Format Numbers as Currency with Python
  • 5.  Using Python to Insert Item Into List
  • 6.  Python Get Operating System Information with os and platform Modules
  • 7.  Symmetric Difference of Two Sets in Python
  • 8.  How to Check if a String Contains Vowels in Python
  • 9.  Check if Set Contains Element in Python
  • 10.  Find Last Occurrence in String of Character or Substring 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