L.S.Jonathan
L.S.Jonathan

Reputation: 11

Find value of a randomly chosen key from dictionary

I have a .py dictionary which I want to use in another file. I want to randomly choose a key

w_keys = dictionary.dic.keys()

def inf():
   return random.choice(list(w_keys))

x = inf()

and then I need to find the value of the chosen key,

value = dictionary.dic.values()
return dictionary.dic[x] == value

but I am not sure how to do so and my attempts (unsurprisingly) don't work?

Upvotes: 1

Views: 67

Answers (1)

Manuel
Manuel

Reputation: 706

import random

dict = {'A': 'a', 'B': 'b'}

def inf(dict):
   return random.choice(list(dict.keys()))

x = dict[inf(dict)]

Upvotes: 1

Related Questions