I am trying to create a dictionary data type using python 3.2. I am using random method to randomly choose a values in the mydict and print out the value. However, I would like to print the key as well (e.g. Animal: elephant) but I only manage to print either the key or the values. How do I go about it?
mydict = {'Animal': ('elephant', 'giraffe', 'rhinoceros', 'hippopotamus', 'leopard'),
'Fruit': ('strawberry', 'mango', 'watermelon', 'orange', 'durian')}
random_word = random.choice(random.choice(list(mydict.values())))
mydict.values(), unsurprisingly, gives you the values. You can usemydict.items()to get the key/value pairs (as tuples).Edit: it sounds like you’re trying to do something like this: