Python Program to Rotate dictionary by K

Python dictionaries are ordered collections of key-value pairs. Sometimes you need to rotate a dictionary by K positions, which means shifting all key-value pairs by K positions in their order. This article demonstrates how to rotate a dictionary by K positions using two different approaches.

Understanding Dictionary Rotation

Dictionary rotation shifts all key-value pairs by K positions. For example, rotating {2: 5, 4: 6, 1: 3} by 1 position gives {1: 3, 2: 5, 4: 6}.

Example Input and Output

# Input dictionary
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
k = 3

print("Original:", inputDict)
print("After 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}")
Original: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
After 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}

Method 1: Using List Comprehension and Dictionary Comprehension

This approach converts the dictionary to a list of tuples, rotates using list indexing, then converts back to a dictionary.

Algorithm

  • Convert dictionary to list of tuples using items()

  • Use list comprehension with modular arithmetic to rotate positions

  • Convert back to dictionary using dictionary comprehension

Example

# Input dictionary
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
print("Input Dictionary:", inputDict)

# Number of rotations
k = 3

# Convert dictionary to list of tuples
items_list = list(inputDict.items())

# Rotate by k positions using list comprehension
rotated_items = [items_list[(i - k) % len(items_list)] for i in range(len(items_list))]

# Convert back to dictionary
resultDict = {key: value for key, value in rotated_items}

print("Resultant dictionary after", k, "rotations:", resultDict)
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}

Method 2: Using collections.deque.rotate()

The deque (double-ended queue) from the collections module provides an efficient rotate() method for rotation operations.

Why Use deque?

Deque offers O(1) time complexity for rotation operations, making it more efficient than manual list manipulation for large datasets.

Example

from collections import deque

# Input dictionary
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
print("Input Dictionary:", inputDict)

# Number of rotations
k = 3

# Convert dictionary to list of tuples
items_list = list(inputDict.items())

# Create deque object
deque_obj = deque(items_list)

# Rotate by k positions
deque_obj.rotate(k)

# Convert back to dictionary
resultDict = {key: value for key, value in deque_obj}

print("Resultant dictionary after", k, "rotations:", resultDict)
Input Dictionary: {2: 5, 4: 6, 1: 3, 9: 4, 5: 1}
Resultant dictionary after 3 rotations: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}

Comparison

Method Time Complexity Best For
List Comprehension O(n) Simple implementation
deque.rotate() O(1) for rotation Large datasets, frequent rotations

Conclusion

Dictionary rotation can be achieved using list comprehension with modular arithmetic or the efficient deque.rotate() method. Use deque.rotate() for better performance with large dictionaries or frequent rotation operations.

Updated on: 2026-03-27T13:01:26+05:30

385 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements