pick random entry in dict python 1

pick random entry in dict python

from random import choice

dictionary[choice(list(dictionary.keys()))] # where "dictionary" is the label of, guess what, the variable holding a dictionary you want a random item from.

Here is what the above code is Doing:
1. It’s creating a list of all the keys in the dictionary.
2. It’s choosing a random item from that list.
3. It’s using that random item as a key to get a value from the dictionary.

Similar Posts