switching keys and values in a dictionary in python

PythonDictionaryKey

Python Problem Overview


Say I have a dictionary like so:

my_dict = {2:3, 5:6, 8:9}

Is there a way that I can switch the keys and values to get:

{3:2, 6:5, 9:8}

Python Solutions


Solution 1 - Python

For Python 3:

my_dict2 = {y: x for x, y in my_dict.items()}

For Python 2, you can use

my_dict2 = dict((y, x) for x, y in my_dict.iteritems())

Solution 2 - Python

my_dict = { my_dict[k]:k for k in my_dict}

Solution 3 - Python

Try this:

my_dict = {2:3, 5:6, 8:9}

new_dict = {}
for k, v in my_dict.items():
	new_dict[v] = k

Solution 4 - Python

Use this code (trivially modified) from the accepted answer at https://stackoverflow.com/questions/483666/python-reverse-inverse-a-mapping:

dict((v,k) for k, v in my_dict.iteritems())

Note that this assumes that the values in the original dictionary are unique. Otherwise you'd end up with duplicate keys in the resulting dictionary, and that is not allowed.

And, as @wim points out, it also assumes the values are hashable. See the Python glossary if you're not sure what is and isn't hashable.

Solution 5 - Python

maybe:

flipped_dict = dict(zip(my_dict.values(), my_dict.keys()))

Solution 6 - Python

First of all it is not guaranteed that this is possible, since the values of a dictionary can be unhashable.

In case these are not, we can use a functional approach with:

reversed_dict = dict(map(reversed, original_dict.items()))

Solution 7 - Python

Sometimes, the condition that the values are all unique will not hold, in which case, the answers above will destroy any duplicate values.

The following rolls the values that might be duplicates up into a list:

from itertools import count
dict([(a,[list(d.keys())[i] for i,j in zip(count(), d.values())if j==a in set(d.values())])

I'm sure there's a better (non-list-comp) method, but I had a problem with the earlier answers, so thought I'd provide my solution in case others have a similar use-case.

P.S. Don't expect the dict to remain neatly arranged after any changes to the original! This method is a one-time use only on a static dict - you have been warned!

Solution 8 - Python

If the values are not unique this will collide the key space in conversion. Best is to keep the keys in list when switching places

below handles this -

RvsD = dict()
for k,v in MyDict.iteritems():
    RsvD.setdefault(v, []).append(k)

Solution 9 - Python

You can do it like this:

Function:

def inverse_dict(my_dict):
    updated_dict = {}

for i in my_dict:
    updated_dict[my_dict[i]] = [i]

return updated_dict

Main():

def main():
    year = {'day': 15, 'month': 3, 'year': 2019}
    print(inverse_dict(year))

if __name__ == "__main__":
    main()

Ouput:

{15: ['day'], 3: ['month'], 2019: ['year']}

Solution 10 - Python

If values are not unique in the dictionary, then you can map keys to that value. Here an example of how to do it using defaultdict

from collections import defaultdict


def reverse_dict(data: dict) -> dict:
    rd = defaultdict(list)
    for k, v in data.items():
        rd[v].append(k)
    return rd


if __name__ == "__main__":
    from collections import Counter

    data = "aaa bbb ccc ddd aaa bbb ccc aaa"
    c = Counter(data.split())
    print(c)
    # Counter({'aaa': 3, 'bbb': 2, 'ccc': 2, 'ddd': 1})
    rd = reverse_dict(c)
    print(rd)
    # defaultdict(<class 'list'>, {3: ['aaa'], 2: ['bbb', 'ccc'], 1: ['ddd']})

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionme45View Question on Stackoverflow
Solution 1 - PythonGWWView Answer on Stackoverflow
Solution 2 - PythonVladEgView Answer on Stackoverflow
Solution 3 - PythonÓscar LópezView Answer on Stackoverflow
Solution 4 - PythonTrottView Answer on Stackoverflow
Solution 5 - PythonaaronView Answer on Stackoverflow
Solution 6 - PythonWillem Van OnsemView Answer on Stackoverflow
Solution 7 - PythonThomas KimberView Answer on Stackoverflow
Solution 8 - PythonmungayreeView Answer on Stackoverflow
Solution 9 - PythonAlon EilatView Answer on Stackoverflow
Solution 10 - PythonVlad BezdenView Answer on Stackoverflow