Skip to content
Advertisement

How do I shuffle the words that are connected to a key in a dictionary?

I have a dictionary

JavaScript

I want to shuffle the values in this dictionary to different keys.
I tried splitting the dictionary into two like this…

JavaScript

And shuffling the values with

JavaScript

But I keep getting a type error:’dict_values’ object is not subscriptable

I want to recreate the dictionary but with shuffled values. Is this the correct way to get there? Any tips? Thanks!

Advertisement

Answer

As mentioned here you can’t reshuffle a dictionary. However, you can create a list of the dictionary’s values, shuffle that, and then create a new dictionary with the same keys and updated values.

JavaScript
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement