Can i shuffle dictionary in python?

Well I think that you can also do it simpler, without lambdas etc.:

from random import shuffle
from collections import OrderedDict 
a = {num-97:chr(num) for num in range(97, 107)}
# a = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j'}
b = list(a.items())
shuffle(b)
a = OrderedDict(b) #Or just stay with b
# a = OrderedDict([(1, 'b'), (6, 'g'), (4, 'e'), (3, 'd'), (5, 'f'), (8, 'i'), (2, 'c'), (7, 'h'), (9, 'j'), (0, 'a')])
# b = [(1, 'b'), (6, 'g'), (4, 'e'), (3, 'd'), (5, 'f'), (8, 'i'), (2, 'c'), (7, 'h'), (9, 'j'), (0, 'a')]

I think that using shuffle makes the code clearer then using sorted with random key.

For shuffling keys only it would make something like:

keys = list(a.keys())
shuffle(keys)
OrderedDict(zip(keys, a.values()))
# OrderedDict([(3, 'a'), (1, 'b'), (9, 'c'), (4, 'd'), (6, 'e'), (8, 'f'), (0, 'g'), (2, 'h'), (5, 'i'), (7, 'j')])

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}

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}


How do you shuffle items in Python?

To shuffle strings or tuples, use random. sample() , which creates a new object. random. sample() returns a list even when a string or tuple is specified to the first argument, so it is necessary to convert it to a string or tuple.

Is dictionary in Python sequential?

A list is an ordered sequence of objects, whereas dictionaries are unordered sets.

How do you shuffle two lists in Python?

Method : Using zip() + shuffle() + * operator In this method, this task is performed in three steps. Firstly, the lists are zipped together using zip(). Next step is to perform shuffle using inbuilt shuffle() and last step is to unzip the lists to separate lists using * operator.

Can you shuffle a tuple in Python?

Shuffling numbers can sometimes prove to be a great help while programming practices. This process can be directly implemented on the data structures which are mutable like lists, but as we know that the tuples are immutable so it cannot be shuffled directly.