Open In App

Python – Rotate dictionary by K

Improve
Improve
Like Article
Like
Save
Share
Report

Given a dictionary perform its reordering by right rotating dictionary keys by K.

Examples:

Input : test_dict = {1:6, 8:1, 9:3, 10:8, 12:6, 4:9}, K = 2 
Output : {12: 6, 4: 9, 1: 6, 8: 1, 9: 3, 10: 8} 
Explanation : After right rotation ( cyclic ) by 2 elements, items are re-arranged.
 

Input : test_dict = {1:6, 8:1, 9:3, 10:8, 12:6, 4:9}, K = 1 
Output : {4: 9, 1: 6, 8: 1, 9: 3, 10: 8, 12: 6} 
Explanation : After right rotation ( cyclic ) by 1 element, items are re-arranged. 

Method #1 : Using list comprehension + items() + dictionary comprehension

In this, we perform the task of conversion of the dictionary to tuples list, and then perform list rotate, post that, result is again converted to dictionary to get resultant keys rotation.

Python3




# Python3 code to demonstrate working of
# Rotate dictionary by K
# Using list comprehension + items() + dictionary comprehension
 
# initializing dictionary
test_dict = {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# converting to tuples list
test_dict = list(test_dict.items())
 
# performing rotate
res = [test_dict[(i - K) % len(test_dict)]
       for i, x in enumerate(test_dict)]
 
# reconverting to dictionary
res = {sub[0]: sub[1] for sub in res}
 
# printing result
print("The required result : " + str(res))


Output:

The original dictionary is : {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9} The required result : {12: 6, 4: 9, 1: 6, 8: 1, 9: 3, 10: 8}

Method #2 : Using deque.rotate() + dictionary comprehension + items()

In this, we perform task of rotating using deque.rotate utility, rest all functionalities are performed in similar ways as per in above method.

Python3




# Python3 code to demonstrate working of
# Rotate dictionary by K
# Using deque.rotate() + dictionary comprehension + items()
from collections import deque
 
# initializing dictionary
test_dict = {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# initializing K
K = 2
 
# converting to tuples list
test_dict = list(test_dict.items())
 
# performing rotate
# using deque
test_dict = deque(test_dict)
test_dict.rotate(K)
test_dict = list(test_dict)
 
# reconverting to dictionary
res = {sub[0]: sub[1] for sub in test_dict}
 
# printing result
print("The required result : " + str(res))


Output:

The original dictionary is : {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9} The required result : {12: 6, 4: 9, 1: 6, 8: 1, 9: 3, 10: 8}

Method #3: Using slicing and concatenation

Algorithm:

  1. Initialize the input dictionary test_dict and the rotation number K.
  2. Get the list of keys and values from the dictionary using the keys() and values() methods, respectively.
  3. Rotate the keys and values list by K places using slicing.
  4. Zip the rotated keys and values list to create a new list of tuples.
  5. Convert the list of tuples to a dictionary using the dict() method.
  6. Store the rotated dictionary in the res variable and print the resulting rotated dictionary.

Python3




test_dict = {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9}
 
K = 2
print("The original dictionary is : " + str(test_dict))
 
keys = list(test_dict.keys())
vals = list(test_dict.values())
 
keys = keys[-K:] + keys[:-K]
vals = vals[-K:] + vals[:-K]
 
res = dict(zip(keys, vals))
 
print("The required result is : " + str(res))


Output

The original dictionary is : {1: 6, 8: 1, 9: 3, 10: 8, 12: 6, 4: 9}
The required result is : {12: 6, 4: 9, 1: 6, 8: 1, 9: 3, 10: 8}

Time complexity: O(n) where n is the number of key-value pairs

This is because of the operations to create the keys and values lists and to zip and convert the list of tuples to a dictionary.

Auxiliary Space: O(n) where n is the number of key-value pairs

This is because operations such as dictionary creation and variable assignments are negligible compared to the size of the input dictionary.



Last Updated : 21 Mar, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads