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

How to Invert a Dictionary in Python Featured Image

Last Updated on

Welcome to the start of the How to Python series. 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. To get started, I’ve decided to kick off the series by writing an article on how to invert a dictionary.

Table of Contents

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']
}

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()))

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_dictionary = {
  'One for All': 'All Might', 
  'Explosion': 'Katsuki Bakugo', 
  'Zero Gravity': 'Ochaco Uraraka'
}

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()}

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

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

As we can see, we lose one of our keys. Perhaps there is a better way to invert a dictionary.

Invert a Dictionary with Defaultdict

Luckily, there is another 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()}

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']
}

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.

All of that said, this solution is considered bad practice (also, I’d love a source better than Stack Overflow). After all, we are not using the dictionary comprehension as intended. It’s meant to generate a dictionary, but we’re using it to modify an external dictionary.

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)

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']
}

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()}

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_inverted_dict[key]}

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

Performance

Recently, I thought it would be fun to start adding performance information to these articles. In particular, I’m interested in comparing these solutions by their run time. To do that, we’ll use the timeit library. Before we can run our metrics, we’ll need to setup our solutions in strings:

setup = """\
from collections import defaultdict
my_dict = {
  'Izuku Midoriya': 'One for All', 
  'Katsuki Bakugo': 'Explosion', 
  'All Might': 'One for All', 
  'Ochaco Uraraka': 'Zero Gravity'
}
"""

map_and_reversed = """\
dict(map(reversed, my_dict.items()))
"""

dict_comprehension = """\
{value: key for key, value in my_dict.items()}
"""

default_dict = """\
my_inverted_dict = defaultdict(list)
{my_inverted_dict[v].append(k) for k, v in my_dict.items()}
"""

brute_force = """\
my_inverted_dict = dict()
for key, value in my_dict.items():
    my_inverted_dict.setdefault(value, list()).append(key)
"""

With our strings in hand, we can run our test:

>>> import timeit
>>> timeit.timeit(stmt=map_and_reversed, setup=setup)
1.7675568590617678
>>> timeit.timeit(stmt=dict_comprehension, setup=setup)
0.4763165415990329
>>> timeit.timeit(stmt=default_dict, setup=setup)
1.5376035760438356
>>> timeit.timeit(stmt=brute_force, setup=setup)
1.6529047465693054

As it turns out, the dictionary comprehension is very fast. However, be aware that it can’t handle non-unique values. If that’s not a constraint, then go with the fast solution. Otherwise, it doesn’t really matter which method you choose. Also, for reference, I ran this on my Windows 10 desktop with Python 3.6.3.

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]}

Just about every other type of dictionary transformation is out of the scope of this tutorial. However, if you have any specific questions, feel free to reach out in the comments.

While you’re here, you might be interested in these articles as well:

Alternatively, you can get the latest articles sent to your inbox by becoming a member. If you’d prefer not to become a member but you’d still like to support the site, head over to Amazon and pick up some My Hero Academia merch:

We appreciate the support!

Series NavigationHow to Sum Elements of Two Lists in Python: Comprehensions and More →
Advertisements

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.