Python: how do I randomly select a value from a dictionary key?

I have a dictionary:

dict = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

and I want to print one random value from each key, so I first tried to include them in the list:

import random

food = [dict.value.random.choice()]

but it doesn't work (no wonder it looks overly confusing)

and I want then printfood:

print food

and just look:

green
squishy
savory

or any other value was randomly selected.

makes the list unnecessary? I will continue to attempt publication.

Just to clarify why this is not a duplicate: I do not want to randomly grab an element from the dictionary, I want to randomly grab an element from each list inside the dictionary.

+4
source share
5

, :

>>> my_dict = {
...     "Apple": ["Green", "Healthy", "Sweet"],
...     "Banana": ["Yellow", "Squishy", "Bland"],
...     "Steak": ["Red", "Protein", "Savory"]
... }
>>> import random
>>> food=[random.choice(i) for i in my_dict.values()]
>>> food
['Savory', 'Green', 'Squishy']

, , join food :

>>> print '\n'.join(food)
Savory
Green
Squishy
>>> for val in food :
...      print val
... 
Savory
Green
Squishy
+5

for:

import random

dict = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

for key, value in dict.items():
    print random.choice(value), key

:

Red Steak
Healthy Apple
Bland Banana
+3

,

import random
choices = [random.choice(v) for k, v in your_dict.items()] # iterate over the dict items
print(choices)

['Protein', 'Green', 'Squishy']
+3

(BTW) "dict" )

# for python3
from random import randint
data = {
  "Apple": ["Green", "Healthy", "Sweet"],
  "Banana": ["Yellow", "Squishy", "Bland"],
  "Steak": ["Red", "Protein", "Savory"]
}

for  key, value in data.items():
  print(key + ":" + value[randint(0,2)])

( int)

sh-4.2# python3 main.py                                                                                                                                                         
Apple:Sweet                                                                                                                                                                     
Steak:Red                                                                                                                                                                       
Banana:Squishy                                                                                                                                                                    

01 ( @)

for  key, value in data.items():
  length = len(value)
  print(key + ":" + value[randint(0,length-1)])
+1

, , , :

import random  

food_char = {
    "Apple": ["Green", "Healthy", "Sweet"],
    "Banana": ["Yellow", "Squishy", "Bland"],
    "Steak": ["Red", "Protein", "Savory"]
}

food=[random.choice(i) for i in food_char.values()]

for item in food:
    print item
0

All Articles