
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Rotate Dictionary by K in Python
Python's implementation of a data structure known more commonly as an associative array is a dictionary. A dictionary is made up of a group of key-value pairs. Each key-value combination corresponds to a key and its corresponding value.
Given a dictionary with some random key-value pairs, In this article, we will learn how to rotate the given dictionary by K in python.
Methods Used
The following are the various methods to accomplish this task:
Using list comprehension, items(), and dictionary comprehension
Using dictionary comprehension, deque.rotate(), and items() functions
Example
Assume we have taken an input dictionary and K rotations. We will now rotate the input dictionary by k times and print the resultant dictionary after k rotations.
Input
inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} k=3
Output
{1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
In the above example, the K value is 3. Input dictionary after:
1st rotation: {5:1, 2: 5, 4: 6, 1: 3, 9: 4}
2nd rotation: {9:4, 5:1, 2: 5, 4: 6, 1: 3}
3rd rotation: {1: 3, 9: 4, 5: 1, 2: 5, 4: 6}
Method 1 Using list comprehension, items(), and dictionary comprehension
In this method, we are going to understand how to use list comprehension and dictionary comprehension to rotate the dictionary by K.
Syntax
enumerate(iterable, start=0)
The enumerate() method adds a counter to an iterable and returns the enumerate object.
Parameters
iterable- It can be any sequence/object/iterable supporting iteration
start- enumerate() begins counting from this value. If the start is not specified, the value 0 is used.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Create a variable to store the input dictionary.
Print the input dictionary.
Create another variable to store the input number of k rotations.
Use the items() function(returns a view object i.e, it contains the key-value pairs of the dictionary, as tuples in a list) to get the key, and value of the dictionary.
Convert this into a list of tuples using the list() function(converts the sequence/iterable to a list)
Traverse in the index, element of the list using the enumerate() function and rotate it by k using the mathematical logic.
Convert the result list of tuples into the dictionary again using dictionary comprehension.
Print the resultant dictionary after rotating the dictionary k times.
Example
The following program returns a dictionary after rotating the input dictionary by given input K rotations using list comprehension, items(), and dictionary comprehension -
# input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} # printing input dictionary print("Input Dictionary:", inputDict) # input K rotations k = 3 # getting the key, and value of the dictionary as a tuple and converting it into the list inputDict = list(inputDict.items()) # rotationg the input dictionary by k rotations resultList = [inputDict[(p - k) % len(inputDict)] for p, m in enumerate(inputDict)] # converting the result list of tuples into a dictionary again # using dictionary comprehension resultantDict = {e[0]: e[1] for e in resultList} # printing the resultant dictionary after given input k rotations print("Resultant dictionary after", k, "rotations:", resultantDict)
Output
On executing, the above program will generate the following output -
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 dictionary comprehension, deque.rotate(), and items() functions
A double-ended queue, also known as a deque, allows users to add and remove items from either end. The Deque module belongs to the collections library. It provides methods that can be used directly with arguments for adding and removing items.
When we need faster append and pop operations from both ends of the container, deque is chosen over lists because deque offers O(1) time complexity for append and pop operations whereas lists offer O(n) time complexity.
deque.rotate() function ? The deque is rotated by the number given in arguments using this function. The rotation occurs to the left if the given number is negative. The rotation will be to the right if not.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task -.
Use the import keyword to import the deque from the collections module.
Use the items() function to get the key, and value pairs of the dictionary.
Convert this into a list of tuples using the list() function(converts the sequence/iterable to a list)
Use the deque() function by passing the input dictionary as an argument to it to convert the dictionary into the deque.
Use the rotate() function by passing the input k value to it and apply it to the deque to rotate the input dictionary by k rotations.
Use the list() function(returns a list of an iterable) to convert the result into a list.
Convert the result list of tuples into the dictionary again using dictionary comprehension.
Print the resultant dictionary after rotating the dictionary k times.
Example
The following program returns a dictionary after rotating the input dictionary by given input K rotations using dictionary comprehension, deque.rotate(), and items() functions-
# importing deque from the collections module from collections import deque # input dictionary inputDict = {2: 5, 4: 6, 1: 3, 9: 4, 5: 1} # printing input dictionary print("Input Dictionary:", inputDict) # input K rotations k = 3 # getting the key, value of dictionary as a tuple # and converting it into the list inputDict = list(inputDict.items()) # Converting input dictionary to deque dequeObject = deque(inputDict) # rotating the deque object by k rotations dequeObject.rotate(k) # converting into the list resultList = list(dequeObject) # converting the result list of tuples into a dictionary again resultantDict = {e[0]: e[1] for e in resultList} # printing the resultant dictionary after input k rotations print("Resultant dictionary after", k, "rotations:", resultantDict)
Output
On executing, the above program will generate the following output -
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}
Conclusion
We learned two distinct ways to rotate a dictionary by K times in this article. We also learned how to use the rotate() function to add rotation to a deque object and how to convert the given dictionary to deque.