How to Invert a Dictionary in Python: Comprehensions, Defaultdict, and More

Jeremy Grifski - Feb 21 '19 - - Dev Community

Article was originally posted on therenegadecoder.com where it's actively maintained.

In this series, I’m putting together several articles for small Python problems that can be solved in a few lines of code. This series is inspired by the everyday Google searches I make to solve my own problems at work. Today, I'm writing an article on how to invert a dictionary.

Problem Introduction

Recently, I was working on a Python project where I needed to invert a dictionary. For all you Boku no Hero Academia (BNHA) fans out there, I basically wanted to be able to do the following:

my_dict = {
  'Izuku Midoriya': 'One for All', 
  'Katsuki Bakugo': 'Explosion', 
  'All Might': 'One for All', 
  'Ochaco Uraraka': 'Zero Gravity'
}

my_inverted_dict = {
  'One for All': ['Izuku Midoriya', 'All Might'], 
  'Explosion': ['Katsuki Bakugo'], 
  'Ochaco Uraraka': ['Zero Gravity']
}
Enter fullscreen mode Exit fullscreen mode

In this scenario, we have a dictionary of characters from BNHA mapped to their quirks, and we want to convert that to a dictionary of quirks mapped to the characters that have them.

Unfortunately, the original dictionary has non-unique values, so flipping the dictionary would result in a loss of keys. Instead, we want to accumulate a list of keys for each non-unique value while performing the inversion. As it turns out, this is pretty easy to do.

Solutions

If we want to invert a dictionary, we have several options. From comprehensions to loops, we’ll take a look at all the practical solutions.

Invert a Dictionary with Map and Reversed

Let’s kick off our series of options with one of the more succinct options:

my_inverted_dict = dict(map(reversed, my_dict.items()))
Enter fullscreen mode Exit fullscreen mode

Here, we use the map function which applies the reversed function to all the items in the dictionary. Then, we take the map object and convert it into a dictionary. The result looks something like the following:

my_inverted_dict = {
  'One for All': 'All Might', 
  'Explosion': 'Katsuki Bakugo', 
  'Zero Gravity': 'Ochaco Uraraka'
}
Enter fullscreen mode Exit fullscreen mode

Of course, if we want to make sure we don’t lose Midoriya, we shouldn’t use this method. Otherwise, this would be a perfect solution.

Invert a Dictionary with a Comprehension

In Python 2.7 and above, we can use a dictionary comprehension to invert a dictionary. Unfortunately, it falls prey to the same issue mentioned before, but it gets the job done for unique values:

my_inverted_dict = {value: key for key, value in my_dict.items()}
Enter fullscreen mode Exit fullscreen mode

Once again, here’s the result of inverting our example dictionary:

my_inverted_dict = {
  'One for All': 'All Might', 
  'Explosion': 'Katsuki Bakugo', 
  'Zero Gravity': 'Ochaco Uraraka'
}
Enter fullscreen mode Exit fullscreen mode

As we can see, we lose one of our keys. So, there has to be a better way to invert a dictionary.

Invert a Dictionary with Defaultdict

Luckily, there is a better way! Thanks to our friend, Niels van Galen Last, we can accomplish exactly what we need in three lines of code:

from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}
Enter fullscreen mode Exit fullscreen mode

Here, we've chosen to leverage the defaultdict object from collections. It allows us to set default values for keys. In this case, we've chosen a default value of list, so we can append data without the risk of a runtime error. Check out the results:

my_inverted_dict = {
  'One for All': ['Izuku Midoriya', 'All Might'], 
  'Explosion': ['Katsuki Bakugo'], 
  'Zero Gravity': ['Ochaco Uraraka']
}
Enter fullscreen mode Exit fullscreen mode

Since defaultdict works like a regular dict, we can interact with it all the same. This is great solution if you don't have any strict dict requirements, and your values are not unique.

Invert a Dictionary with a For Loop

Another way to invert a dictionary is to use a for loop. This allows us to iterate over the set of mappings and properly build the new mappings by hand. Take a look:

my_inverted_dict = dict()
for key, value in my_dict.items():
    my_inverted_dict.setdefault(value, list()).append(key)
Enter fullscreen mode Exit fullscreen mode

With this method, we can invert a dictionary while preserving all of our original keys. Let’s take a look at what would happen if we ran this code snippet:

my_inverted_dict = {
  'One for All': ['Izuku Midoriya', 'All Might'], 
  'Explosion': ['Katsuki Bakugo'], 
  'Zero Gravity': ['Ochaco Uraraka']
}
Enter fullscreen mode Exit fullscreen mode

Great! We have exactly what we need, but what happens if we want to return this dictionary to its original form?

Revert the Inversion

In the basic case where all keys and values are unique, we can revert a dictionary back to its original mapping using the same dictionary comprehension we’ve already covered:

my_dict = {value: key for key, value in my_inverted_dict.items()}
Enter fullscreen mode Exit fullscreen mode

Unfortunately, this doesn’t work out with a dictionary that maps keys to lists. That’s because lists in Python are unhashable types. In other words, Python doesn’t allow lists to be keys in dictionaries because lists are not immutable.

Fortunately, it’s easier to revert our dictionary than it was to invert it in the first place. We can use the following dictionary comprehension:

my_dict = {value: key for key in my_inverted_dict for value in my_map[key]}
Enter fullscreen mode Exit fullscreen mode

As we can see, we make a new key-value pair for every single value in each list using this double loop structure.

A Little Recap

Using the methods above, we can invert just about any dictionary.

# Use to invert dictionaries that have unique values
my_inverted_dict = dict(map(reversed, my_dict.items()))

# Use to invert dictionaries that have unique values
my_inverted_dict = {value: key for key, value in my_dict.items()}

# Use to invert dictionaries that have non-unique values
from collections import defaultdict
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}

# Use to invert dictionaries that have non-unique values
my_inverted_dict = dict()
    for key, value in my_dict.items():
        my_inverted_dict.setdefault(value, list()).append(key)

# Use to invert dictionaries that have lists of values
my_dict = {value: key for key in my_inverted_dict for value in my_map[key]}
Enter fullscreen mode Exit fullscreen mode

Just about every other type of dictionary transformation is out of the scope of this tutorial. However, if you have any specific questions, consider dropping them down below in the comments. I'm always happy to write an article for someone!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player