3

I have a dictionary and I want to shuffle the values, not the keys. For example:

{"a": "ACAT", "b": "ACTG", "c": "ACCC"}

and after shuffle:

{"a": "ACAT", "b": "ACTG", "c": "ACCC"}

but I don't know how I can do this work in python. I will be grateful if you help me.

4 Answers 4

3

The random module has a function that shuffles in-place.

So we can get the dict's values, shuffle them, and construct a new dict.

Demo:

>>> import random
>>> d = {
...     "a": "ACAT",
...     "b": "ACTG",
...     "c": "ACCC"
... }
>>> shuffled = list(d.values())
>>> random.shuffle(shuffled)
>>> dict(zip(d, shuffled))
{'a': 'ACCC', 'b': 'ACTG', 'c': 'ACAT'}
5
  • Is it possible to do the same with a key? For example, suppose two people have the same dictionary and want to shuffle their dictionaries with one key and reach the same dictionaries. Is it possible?
    – ph9675
    Jan 8, 2021 at 10:21
  • @parisan I don't understand the question.
    – timgeb
    Jan 8, 2021 at 11:21
  • Suppose,I and you have the same dictionary, we are far apart but we want to shuffle dictionaries in such a way that they change the same way. Is there any way?
    – ph9675
    Jan 8, 2021 at 17:50
  • @parisan use the same random seed.
    – timgeb
    Jan 9, 2021 at 18:51
  • Thanks, but it does not give a different output each time?
    – ph9675
    Jan 11, 2021 at 8:42
3

You can use random.sample to get a random ordered list of the dict's values, then map them with the keys in their default order. At the difference of using shuffle you can do this in once

from random import sample

result = dict(zip(values, sample(list(values.values()), len(values))))

Run that 10 times

{'a': 'ACAT', 'b': 'ACCC', 'c': 'ACTG'}
{'a': 'ACTG', 'b': 'ACCC', 'c': 'ACAT'}
{'a': 'ACCC', 'b': 'ACTG', 'c': 'ACAT'}
{'a': 'ACTG', 'b': 'ACAT', 'c': 'ACCC'}
{'a': 'ACAT', 'b': 'ACCC', 'c': 'ACTG'}
{'a': 'ACCC', 'b': 'ACTG', 'c': 'ACAT'}
{'a': 'ACCC', 'b': 'ACTG', 'c': 'ACAT'}
3
  • 1
    I suggest dict(zip(...)).
    – timgeb
    Jan 8, 2021 at 9:51
  • 1
    @timgeb thanks I don't have that reflex as I so use list/dict comprehension :D
    – azro
    Jan 8, 2021 at 9:52
  • Also I just realized calling .keys() is unnecessary, dicts are iterables of their keys.
    – timgeb
    Jan 8, 2021 at 9:54
0

You can use a for loop and random.shuffle to shuffle the values of the dict, while changing the original dict.

Code:

import random

d = {"a": "ACAT", "b": "ACTG", "c": "ACCC"}

vals = list(d.values())
random.shuffle(vals)

for i, key in enumerate(d): d[key] = vals[i]

Explanation:

  • import random, importing the random module
  • d = {"a": "ACAT", "b": "ACTG", "c": "ACCC"}, setting d to a dict
  • vals = list(d.values()), setting val to a list of all values of d
  • random.shuffle(vals), shuffling vals
  • for i, key in enumerate(d): d[key] = vals[i], changing d to the random values
0

You can't really shuffle the values of the dictionary by simple writing:

shuffle(my_dict.values())

This will raise TypeError. But what you can do is to convert dictionary keys and values into a list, and then shuffle. Try the following code:

from random import shuffle

my_dict = {
    "a": "ACAT",
    "b": "ACTG",
    "c": "ACCC",
}

key_list = list(my_dict.keys())
values_list = list(my_dict.values())
shuffle(values_list)

for i in range(len(key_list)):
    my_dict[key_list[i]] = values_list[i]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.