How to Reverse a Dictionary in Python Last Updated : 10 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report Reversing a dictionary typically means swapping the keys and values, where the keys become the values and the values become the keys. In this article, we will explore how to reverse a dictionary in Python using a variety of methods.Using Dictionary ComprehensionDictionary comprehension is a concise way to swap key and values by creating a new dictionary. Python d1 = {'a': 1, 'b': 2, 'c': 3} # Reverse the dictionary using dictionary comprehension d2 = {v: k for k, v in d1.items()} print(d2) Output{1: 'a', 2: 'b', 3: 'c'} Explanation:We use the items() method to iterate over each key-value pair in the original dictionary.For each pair, we create a new dictionary where the value becomes the key and the key becomes the value.Let's explore other methods of reversing a dictionary in python:Table of ContentUsing zip() FunctionUsing a LoopUsing zip() FunctionWe can use Python’s built-in zip() function to reverse a dictionary. zip() function can combine two sequences (keys and values) into pairs, which can then be converted back into a dictionary. Python d1 = {'a': 1, 'b': 2, 'c': 3} # Reverse the dictionary using zip d2 = dict(zip(d1.values(), d1.keys())) print(d2) Output{1: 'a', 2: 'b', 3: 'c'} Explanation:The zip() function pairs the values of d1.values() with the keys of d1.keys().The result is a series of tuples that are converted back into a dictionary using the dict() constructor.Using a LoopWe can also reverse a dictionary using a simple loop, which gives you more control over the process if needed. Python d1 = {'a': 1, 'b': 2, 'c': 3} d2 = {} # Loop through the dictionary and swap keys and values for key, value in d1.items(): d2[value] = key print(d2) Output{1: 'a', 2: 'b', 3: 'c'} Explanation:We iterate over each key-value pair in the original dictionary.In each iteration, we assign the value as the new key in the reversed dictionary and the key as the value. Comment More infoAdvertise with us Next Article How to Print Dictionary Keys in Python A anuragtriarna Follow Improve Article Tags : Python Python Programs python-dict Python dictionary-programs Practice Tags : pythonpython-dict Similar Reads Python - Assign Reversed Values in Dictionary Given a dictionary, assign each key, values after reverting the values of dictionary. Input : {1 : 4, 2 : 5, 3 : 6} Output : {1 : 6, 2 : 5, 3 : 4} Explanation : Value order changed, 4, 5, 6 to 6, 5, 4. Input : {1 : 5, 2 : 5, 3 : 5} Output : {1 : 5, 2 : 5, 3 : 5} Explanation : Same values, no visible 4 min read Reverse Dictionary Keys Order - Python We are given a dictionary and our task is to reverse the order of its keys. This means if we have a dictionary like {'a': 1, 'b': 2, 'c': 3} then the output will be {'c': 3, 'b': 2, 'a': 1}. This can be done using methods like reversed(), dictionary comprehensions, or OrderedDict. Let's explore thes 4 min read Reverse Dictionary Keys Order - Python We are given a dictionary and our task is to reverse the order of its keys. This means if we have a dictionary like {'a': 1, 'b': 2, 'c': 3} then the output will be {'c': 3, 'b': 2, 'a': 1}. This can be done using methods like reversed(), dictionary comprehensions, or OrderedDict. Let's explore thes 4 min read How to Print Dictionary Keys in Python We are given a dictionary and our task is to print its keys, this can be helpful when we want to access or display only the key part of each key-value pair. For example, if we have a dictionary like this: {'gfg': 1, 'is': 2, 'best': 3} then the output will be ['gfg', 'is', 'best']. Below, are the me 2 min read Python Print Dictionary Keys and Values When working with dictionaries, it's essential to be able to print their keys and values for better understanding and debugging. In this article, we'll explore different methods to Print Dictionary Keys and Values.Example: Using print() MethodPythonmy_dict = {'a': 1, 'b': 2, 'c': 3} print("Keys:", l 2 min read Python Program to Reverse a Number We are given a number and our task is to reverse its digits. For example, if the input is 12345 then the output should be 54321. In this article, we will explore various techniques for reversing a number in Python. Using String SlicingIn this example, the Python code reverses a given number by conve 3 min read Like