How to reverse dictionary mapping in Python

In Python, a dictionary is a data structure that stores key-value pairs. In other programming languages, it is also known as an associative array, hash map, or hash table (due to the presence of key and value). Dictionaries are mutable, unordered collections of items, where each item consists of a key-value pair.

Reversing a Python dictionary involves altering the arrangement of values within each key-value pair. In this transformation, the existing key in each pair assumes the role of the value, while the current value becomes the corresponding key in the resultant dictionary. To illustrate, consider the following example dictionary:

canvasAnimation-image
1 of 4

In this Answer, we will discuss different methods to reverse a dictionary in Python.

Using dictionary comprehension

We can reverse the key-value pairs of a dictionary using dictionary comprehension. This method is concise and efficient.

# initializing the dictionary
original_dict = {"Alpha": 1, "Beta": 2, "Gamma": 3}
# reversing the dictionary
reversed_dict = {v: k for k, v in original_dict.items()}
# printing original and reversed dictionary
print ("Original Dictionary Values", original_dict)
print("Reversed Dictionary Values", reversed_dict)

Code explanation

Let’s discuss the above code step-by-step:

  • Line 2: We initialize a dictionary original_dict with key-value pairs.

  • Line 4: We use dictionary comprehension to reverse the key-value mapping of original_dict, where each key-value pair is swapped so that the values become keys and the keys become values in the reversed_dict.

  • Lines 7 and 8: We print the original and the reversed dictionary.

Using a loop

We can iterate over the items of the original dictionary and build a new dictionary with swapped keys and values.

# initializing dictionary
original_dict = {"Alpha": 1, "Beta": 2, "Gamma": 3}
# creating an empty dictionary
reversed_dict = {}
# using for loop to reverse dictionary mapping
for key, value in original_dict.items():
reversed_dict[value] = key
# printing original and reversed dictionary
print ("Original Dictionary Values", original_dict)
print("Reversed Dictionary Values", reversed_dict)

Code explanation

Let’s discuss the above code step-by-step:

  • Line 2: We initialize a dictionary original_dict with key-value pairs.

  • Line 5: We create an empty dictionary reversed_dict.

  • Lines 8 and 9: We use for loop to iterate over each key-value pair in the dictionary original_dict. For each iteration, it assigns the key to the variable key and the value to the variable value. Then, it creates a key-value pair in the reversed_dict, where the key is the original value and the value is the original key, effectively swapping the keys and values to reverse the mapping.

  • Lines 12 and 13: We print the original and the reversed dictionary.

Using the lambda function

We can use the lambda function to swap the values of keys and values in a dictionary.

# initializing dictionary
original_dict = {"Alpha": 1, "Beta": 2, "Gamma": 3}
# inverse mapping using lambda
reversed_dict = (lambda original_dict: {v:k for k, v in original_dict.items()})(original_dict)
# print initial and reversed dictionary
print("Original Dictionary Values", original_dict)
print("Reversed Dictionary Values", reversed_dict)

Code explanation

Let’s discuss the above code step-by-step:

  • Line 2: We initialize a dictionary original_dict with key-value pairs.

  • Line 5: We define a lambda function that takes a dictionary d and returns a new dictionary with the keys and values swapped. This lambda function is immediately applied to the original_dict using (lambda d: {v: k for k, v in d.items()})(original_dict).

  • Lines 8 and 9: We print the original and the reversed dictionary.

Using the zip() function

We can use the zip() function to swap keys and values, then convert the result into a dictionary.

# initializing dictionary
original_dict = {"Alpha": 1, "Beta": 2, "Gamma": 3}
# using zip() function to reverse dictionary
reversed_dict = dict(zip(original_dict.values(), original_dict.keys()))
# printing original and reversed dictionary
print ("Original Dictionary Values", original_dict)
print("Reversed Dictionary Values", reversed_dict)

Code explanation

Let’s discuss the abovementioned code step-by-step:

  • Line 2: We initialize a dictionary original_dict with key-value pairs.

  • Line 5: We use the zip() function to swap the keys and values of the original_dict, effectively reversing the mapping.

  • Lines 8 and 9: We print the original and the reversed dictionary.

Conclusion

Reversing dictionary mapping in Python is a basic operation that enables the transformation of key-value pairs, swapping their roles to facilitate efficient lookup operations in reverse. This process is essential in various programming tasks, including data transformation, analysis, algorithmic problem-solving, and database operations. Using dictionary comprehension, loop iterations, the lambda function, and the zip() function, developers can easily reverse the mapping of dictionaries. In this way, programmers can enhance their ability to manipulate and manage data effectively, thereby contributing to more robust and efficient software solutions.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved