Monday, November 18, 2024
Google search engine
HomeLanguagesPython – Shuffle dictionary Values

Python – Shuffle dictionary Values

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, “Lazyroar” : 9}
Output : {“gfg” : 9, “is” : 8, “best” : 7, “for” : 3, “Lazyroar” : 1}
Explanation : Keys are at same position but values are shuffled.

Input : test_dict = {“gfg” : 7, “is” : 1, “best” : 8, “for” : 3, “Lazyroar” : 9}
Output : {“gfg” : 9, “is” : 8, “best” : 7, “for” : 3, “Lazyroar” : 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




# Python3 code to demonstrate working of
# Shuffle dictionary Values
# Using shuffle() + zip() + dict()
import random
 
# initializing dictionary
test_dict = {"gfg": 1, "is": 7, "best": 8,
             "for": 3, "Lazyroar": 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# shuffling values
temp = list(test_dict.values())
random.shuffle(temp)
 
# reassigning to keys
res = dict(zip(test_dict, temp))
 
# printing result
print("The shuffled dictionary : " + str(res))


Output:

The original dictionary is : {‘gfg’: 1, ‘is’: 7, ‘best’: 8, ‘for’: 3, ‘Lazyroar’: 9}

The shuffled dictionary : {‘gfg’: 1, ‘is’: 7, ‘best’: 3, ‘for’: 9, ‘Lazyroar’: 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




# Python3 code to demonstrate working of
# Shuffle dictionary Values
# Using sample() + zip()
from random import sample
 
# initializing dictionary
test_dict = {"gfg": 1, "is": 7, "best": 8,
             "for": 3, "Lazyroar": 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# reassigning to keys
res = dict(zip(test_dict, sample(list(test_dict.values()),
                                 len(test_dict))))
 
# printing result
print("The shuffled dictionary : " + str(res))


Output:

The original dictionary is : {‘gfg’: 1, ‘is’: 7, ‘best’: 8, ‘for’: 3, ‘Lazyroar’: 9}

The shuffled dictionary : {‘gfg’: 8, ‘is’: 9, ‘best’: 1, ‘for’: 3, ‘Lazyroar’: 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
 
# initializing dictionary
test_dict = {"gfg": 1, "is": 7, "best": 8, "for": 3, "Lazyroar": 9}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# shuffling values
values_list = list(test_dict.values())
shuffled_values = random.sample(values_list, len(values_list))
shuffled_dict = dict(zip(test_dict.keys(), shuffled_values))
 
# printing shuffled dictionary
print("The shuffled dictionary : " + str(shuffled_dict))


Output

The original dictionary is : {'gfg': 1, 'is': 7, 'best': 8, 'for': 3, 'Lazyroar': 9}
The shuffled dictionary : {'gfg': 9, 'is': 3, 'best': 7, 'for': 8, 'Lazyroar': 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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments