Python – Shuffle dictionary Values
Last Updated :
02 May, 2023
Given a dictionary, the task is to write a python program to shuffle its values to different keys.
Examples:
Input: test_dict = {“gfg” : 1, “is” : 7, “best” : 8, “for” : 3, “geeks” : 9}
Output : {“gfg” : 9, “is” : 8, “best” : 7, “for” : 3, “geeks” : 1}
Explanation : Keys are at same position but values are shuffled.
Input : test_dict = {“gfg” : 7, “is” : 1, “best” : 8, “for” : 3, “geeks” : 9}
Output : {“gfg” : 9, “is” : 8, “best” : 7, “for” : 3, “geeks” : 1}
Explanation : Keys are at same position but values are shuffled.
Method #1 : Using shuffle() + zip() + dict()
In this, we perform the task of shuffling elements using shuffle(), and zip() is used to map the shuffled values to keys. In the end, dict() is used to convert the result to a dictionary.
Python3
import random
test_dict = { "gfg" : 1 , "is" : 7 , "best" : 8 ,
"for" : 3 , "geeks" : 9 }
print ( "The original dictionary is : " + str (test_dict))
temp = list (test_dict.values())
random.shuffle(temp)
res = dict ( zip (test_dict, temp))
print ( "The shuffled dictionary : " + str (res))
|
Output:
The original dictionary is : {‘gfg’: 1, ‘is’: 7, ‘best’: 8, ‘for’: 3, ‘geeks’: 9}
The shuffled dictionary : {‘gfg’: 1, ‘is’: 7, ‘best’: 3, ‘for’: 9, ‘geeks’: 8}
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using sample() + zip()
In this, the task of shuffling values is done using sample() of random library.
Python3
from random import sample
test_dict = { "gfg" : 1 , "is" : 7 , "best" : 8 ,
"for" : 3 , "geeks" : 9 }
print ( "The original dictionary is : " + str (test_dict))
res = dict ( zip (test_dict, sample( list (test_dict.values()),
len (test_dict))))
print ( "The shuffled dictionary : " + str (res))
|
Output:
The original dictionary is : {‘gfg’: 1, ‘is’: 7, ‘best’: 8, ‘for’: 3, ‘geeks’: 9}
The shuffled dictionary : {‘gfg’: 8, ‘is’: 9, ‘best’: 1, ‘for’: 3, ‘geeks’: 7}
Method 3: use a list comprehension and the random.sample() method.
Step-by-step approach
- Import the random module.
- Initialize the dictionary to be shuffled.
- Create a list of the values of the dictionary using the values() method.
- Use list comprehension to generate a new list of shuffled values using the random.sample() method and the length of the dictionary.
- Use the zip() method to pair the shuffled list with the keys of the dictionary.
- Create a new dictionary with the shuffled pairs using the dict() method.
- Print the original dictionary and the shuffled dictionary.
Python3
import random
test_dict = { "gfg" : 1 , "is" : 7 , "best" : 8 , "for" : 3 , "geeks" : 9 }
print ( "The original dictionary is : " + str (test_dict))
values_list = list (test_dict.values())
shuffled_values = random.sample(values_list, len (values_list))
shuffled_dict = dict ( zip (test_dict.keys(), shuffled_values))
print ( "The shuffled dictionary : " + str (shuffled_dict))
|
Output
The original dictionary is : {'gfg': 1, 'is': 7, 'best': 8, 'for': 3, 'geeks': 9}
The shuffled dictionary : {'gfg': 9, 'is': 3, 'best': 7, 'for': 8, 'geeks': 1}
Time complexity: O(nlogn) for the random.sample() method which internally uses a Fisher-Yates shuffle algorithm.
Auxiliary space: O(n) for the list of values and the list of shuffled values
Share your thoughts in the comments
Please Login to comment...