Python – Rotate dictionary by K
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
test_dict = { 1 : 6 , 8 : 1 , 9 : 3 , 10 : 8 , 12 : 6 , 4 : 9 }
print ( "The original dictionary is : " + str (test_dict))
K = 2
test_dict = list (test_dict.items())
res = [test_dict[(i - K) % len (test_dict)]
for i, x in enumerate (test_dict)]
res = {sub[ 0 ]: sub[ 1 ] for sub in res}
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
from collections import deque
test_dict = { 1 : 6 , 8 : 1 , 9 : 3 , 10 : 8 , 12 : 6 , 4 : 9 }
print ( "The original dictionary is : " + str (test_dict))
K = 2
test_dict = list (test_dict.items())
test_dict = deque(test_dict)
test_dict.rotate(K)
test_dict = list (test_dict)
res = {sub[ 0 ]: sub[ 1 ] for sub in test_dict}
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:
- Initialize the input dictionary test_dict and the rotation number K.
- Get the list of keys and values from the dictionary using the keys() and values() methods, respectively.
- Rotate the keys and values list by K places using slicing.
- Zip the rotated keys and values list to create a new list of tuples.
- Convert the list of tuples to a dictionary using the dict() method.
- 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
Share your thoughts in the comments
Please Login to comment...