Signup/Sign In

How to reverse python dictionary?

In this article, we will learn how to reverse a dictionary using built-in functions such as reversed(), map(), zip(), and comprehension methods.

The reversed() function accepts the parameter sequence and returns the sequence in reverse order.

The map() method applies a given function on each item of iterable and the iterable can be a list, tuple, set, or frozen set.

It returns results according to the iterable.

The zip() function accepts an iterable object and returns an iterable object and the elements from the passed iterator are paired together. It returns a list of the iterable.

Example: Reverse a dictionary using Comprehension

The comprehension concept is also applicable to the dictionary. This method is easy, it just exchanges key and value pairs.

#Dictionary with key-value pairs
dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"}
print("Dictionary:",dict_1)
#Using comprehension method 
x={v:k for k,v in dict_1.items()}
print("Reversed dictionary:",x)

Once we run the code, it shows the following result.


Dictionary: {100: 'python', 200: 'Java', 300: 'Ruby', 400: 'C', 500: 'C++', 600: 'R'}
Reversed dictionary: {'python': 100, 'Java': 200, 'Ruby': 300, 'C': 400, 'C++': 500, 'R': 600}

Example: Reverse a dictionary using reversed() and map() function.

The below example shows how to reverse the dictionary using the map() method and reversed() function.

#Dictionary with key-value pairs
dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"}
print("Dictionary:",dict_1)
#using the map() method
output=map(reversed,dict_1.items())
print(output)
dict_2=dict(output)
#output
print("Reversed Dictionary:",dict_2)

In the above example, first, we defined the dictionary with key-value pairs.

Next, we are using the map() method to reverse the dictionary. Here, in the parameter section, we gave the function as reversed and iterable as dict_1.items.

We are storing the map() method output in the variable output.

In the next step, we are converting them again into the dictionary data type.

Once we run the code, it shows the following result.


Dictionary: {100: 'python', 200: 'Java', 300: 'Ruby', 400: 'C', 500: 'C++', 600: 'R'}
Reversed Dictionary: {'python': 100, 'Java': 200, 'Ruby': 300, 'C': 400, 'C++': 500, 'R': 600}

Example: Reverse a dictionary using the zip() function.

The below example shows how to reverse a dictionary using the zip() function.

#Dictionary with key-value pairs
dictionary_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"}
print("Dictionary:",dictionary_1)
print("-----Getting values from dictionary-----")
get_values=dictionary_1.values()
print("Values from dictionary:",get_values)
print("-----Getting keys from dictionary-----")
get_keys=dictionary_1.keys()
print("Values from dictionary:",get_keys)
print("Using the zip() function")
zip_fun=zip(get_values,get_keys)
dictionary_2=dict(zip_fun)
#output  
print("Reversed Dictiionary:",dictionary_2)

In the above example, we defined the dictionary with key-value pairs. Using the values() method, we are getting the values from the dictionary and that will store in the variable get_values.

Using the keys() method, we are getting the keys from the dictionary and that will store in the variable get_keys.

In the next step, we are passing a sequence of values and keys to the zip() function. The zip() function accepts these sequences and the elements present in the sequence will be paired together.

Next, we are converting the zip() function output to the dictionary.

Once we run the code, it shows the following result.


Dictionary: {100: 'python', 200: 'Java', 300: 'Ruby', 400: 'C', 500: 'C++', 600: 'R'}
-----Getting values from dictionary-----
Values from dictionary: dict_values(['python', 'Java', 'Ruby', 'C', 'C++', 'R'])
-----Getting keys from dictionary-----
Values from dictionary: dict_keys([100, 200, 300, 400, 500, 600])
Using the zip() function
Reversed Dictiionary: {'python': 100, 'Java': 200, 'Ruby': 300, 'C': 400, 'C++': 500, 'R': 600}

Example: Reverse a dictionary without built-in functions.

The below example shows how to reverse a dictionary using for loop.

#Dictionary with key-value pairs
dict_1={100:"python",200:"Java",300:"Ruby",400:"C",500:"C++",600:"R"}
print("Dictionary:",dict_1)
d_temp={}
index=0
for i in dict_1.values():
    d_temp[i]=list(dict_1.keys())[index]
    index+=1
print("Reversed dictionary:",d_temp)

Once we run the code, it shows the following result.


Dictionary: {100: 'python', 200: 'Java', 300: 'Ruby', 400: 'C', 500: 'C++', 600: 'R'}
Reversed dictionary: {'python': 100, 'Java': 200, 'Ruby': 300, 'C': 400, 'C++': 500, 'R': 600}

Conclusion

In this tutorial, we learned to reverse the python dictionaries using the comprehension method, reversed(), map(), zip() functions, and using the for loop.



About the author:
An enthusiastic fresher, a patient person who loves to work in diverse fields. I am a creative person and always present the work with utmost perfection.