Open In App

Python – Assign Reversed Values in Dictionary

Last Updated : 13 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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 change.

Method #1 :  Using values() + loop + reversed()

This is one of the ways in which this task can be performed. In this, we reverse all the values in dictionary using reversed() and reassign to keys. 

Python3




# Python3 code to demonstrate working of
# Assign Reversed Values in Dictionary
# Using reversed() + loop + values()
 
# initializing dictionary
test_dict = {1 : "Gfg", 2 : "is", 3 : "Best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# extract values using values()
new_val = list(reversed(list(test_dict.values())))
 
# reassign new values
res = dict()
cnt = 0
for key in test_dict:
    res[key] = new_val[cnt]
    cnt += 1
 
# printing result
print("Reassigned reverse values : " + str(res))


Output

The original dictionary is : {1: 'Gfg', 2: 'is', 3: 'Best'}
Reassigned reverse values : {1: 'Best', 2: 'is', 3: 'Gfg'}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in dictionary.

Method #2 : Using dictionary comprehension + reversed() + values()

The combination of above functions can be used to solve this problem. In this, we perform the task of remaking reversed dictionary using dictionary comprehension recipe for one-liner solution.

Python3




# Python3 code to demonstrate working of
# Assign Reversed Values in Dictionary
# Using dictionary comprehension + reversed() + values()
 
# initializing dictionary
test_dict = {1 : "Gfg", 2 : "is", 3 : "Best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# extract values using values()
new_val = list(reversed(list(test_dict.values())))
 
# one-liner dictionary comprehension approach
# enumerate for counter
res = {key : new_val[idx] for idx, key in enumerate(list(test_dict.keys()))}
 
# printing result
print("Reassigned reverse values : " + str(res))


Output

The original dictionary is : {1: 'Gfg', 2: 'is', 3: 'Best'}
Reassigned reverse values : {1: 'Best', 2: 'is', 3: 'Gfg'}

Method 3: Using the zip() function and a for loop:

  • Initialize the input dictionary.
  • Extract the keys and values from the dictionary using the keys() and values() methods respectively, and store them in separate lists.
  • Reverse the order of the values list using the reversed() function and convert it to a list using the list() function.
  • Use the zip() function to create an iterable of tuples, where each tuple contains a key-value pair from the input dictionary.
  • Use a for loop to iterate through the iterable of tuples and assign each key in the new dictionary to the corresponding reversed value from the values list.
  • Return the new dictionary with reversed values.

Python3




# Python3 code to demonstrate working of
# Assign Reversed Values in Dictionary
# Using zip() + reversed()
 
# initializing dictionary
test_dict = {1 : "Gfg", 2 : "is", 3 : "Best"}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# extract keys and values using keys() and values()
keys = list(test_dict.keys())
values = list(reversed(list(test_dict.values())))
 
# using zip() to create a dictionary
res = {}
for key, value in zip(keys, values):
    res[key] = value
 
# printing result
print("Reassigned reverse values : " + str(res))


Output

The original dictionary is : {1: 'Gfg', 2: 'is', 3: 'Best'}
Reassigned reverse values : {1: 'Best', 2: 'is', 3: 'Gfg'}

Time complexity: O(n), where n is the number of key-value pairs in the dictionary.
Auxiliary space: O(n), to store the keys and values in lists.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads