Charles Watson
Charles Watson

Reputation: 1075

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 tried to get them into a list first:

import random

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

but this doesn't work (no surprise, it looks excessive and confusing)

and I want to then print food:

print food

and just see:

green
squishy
savory

or whatever value was randomly selected.

is creating the list unnecessary? I'll keep posting attempts.

Just to clarify why this is not a duplicate: I don't want to randomly grab an item from a dictionary, I want to randomly grab an item from a each list inside a dictionary.

Upvotes: 5

Views: 15580

Answers (5)

Charles Watson
Charles Watson

Reputation: 1075

After thoroughly going through docs and using all of the very useful answers provided above, this is what I found to be the most clean and obvious:

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

Upvotes: 0

user4700203
user4700203

Reputation:

(BTW-Change the name 'dict' to something else)

# 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)])

Output (will change depending on the random int values)

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

Amendment 01 (@ Selcuk's question)

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

Upvotes: 1

Malik Brahimi
Malik Brahimi

Reputation: 16711

Use a list comprehension as in

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

Output

['Protein', 'Green', 'Squishy']

Upvotes: 3

Selcuk
Selcuk

Reputation: 59184

You can also use a for loop:

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

result:

Red Steak
Healthy Apple
Bland Banana

Upvotes: 3

Kasravnd
Kasravnd

Reputation: 107287

You can use a list comprehension to loop over your values :

>>> 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']

And for print like what you want you can use join function or loop over food and print the elements one by one :

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

Upvotes: 6

Related Questions