0

I'm trying to get a random value from one of the ice-cream flavors. I've tried using random, pop items, but they all print out the entire line. While I'm trying to just get one of the values (cone flavors) and a flavor.

dict = {
    "vanilla": ["vanilla-cone", "chocolate-cone","cookie-dough-cone", "mint-cone", "rainbow-cone"],
    "Chocolate": ["vanilla-cone", "chocolate-cone","cookie-dough-cone", "mint-cone", "rainbow-cone"],
    "Cookie-dough": ["vanilla-cone", "chocolate-cone","cookie-dough-cone", "mint-cone", "rainbow-cone"],
    "Mint-Chocolate": ["vanilla-cone", "chocolate-cone","cookie-dough-cone", "mint-cone", "rainbow-cone"],
    "Rainbow": ["vanilla-cone", "chocolate-cone","cookie-dough-cone", "mint-cone", "rainbow-cone"]
}

print (dict.popitem())
or 
print(random.choice(list(dict.items())))

Expected Output:
{"Mint-Chocolate": "vanilla-cone"}

4 Answers4

1

Try the following code after your code:

flavor = random.choice(list(dict))
seq = ["vanilla-cone", "chocolate-cone","cookie-dough-cone", 
"mint-cone", "rainbow-cone"]
cone = random.choice(seq)
print(flavor, ":", cone)
Shri B
  • 28
  • 3
0

this is happening because dict.items return pairs of key value tuple, if you want spesific flavor you need to choose a key and than choose again from your list of flavor for spesific flavor try this code

print(random.choice(dict[random.choice(list(dict.keys()))]))

also you can choose directly from the values like this

print(random.choice(random.choice(list(dict.values()))))
jonathan
  • 269
  • 1
  • 7
0

Do it in 2 steps, first get a key, then a value. Also don't name it dict, that the dictionnary constructor keyword

import random

# pick a random key
rand_key = random.choice(list(values))
# pick a random value between the values given by the key
rand_value = random.choice(values[rand_key])

print(rand_key, rand_value)

Here some results after some run

Rainbow cookie-dough-cone
Mint-Chocolate chocolate-cone
Cookie-dough chocolate-cone

To get a dict output,

  • use dict literal instanciation

    rand_key = random.choice(list(values))
    result = {rand_key: random.choice(values[rand_key])}
     # {'Chocolate': 'chocolate-cone'}
    
  • or a dict-comprehension

    result = {k: random.sample(values[k], 1)[0] for k in random.sample(list(values), 1)}
    
azro
  • 53,056
  • 7
  • 34
  • 70
  • Is there a possibility to use the dict constructor keyword directly? Like the list keyword – Anlact Huynh Jan 10 '21 at 16:07
  • @AnlactHuynh Why so ? To get your expected output do `result = {rand_key: random.choice(values[rand_key])}` – azro Jan 10 '21 at 16:10
  • Nothing particular, I felt that it can be used faster? Also, the string of code you just sent returned ```result = {rand_key: random.choice(values[rand_key])} NameError: name 'rand_key' is not defined``` – Anlact Huynh Jan 10 '21 at 17:24
  • @AnlactHuynh `rand_key` was defined in the previous code. You can think about accepting the answer if it satisfies you ;) – azro Jan 10 '21 at 18:23
0

I think your use of dictionary is wrong, because if I understand correctly you want to create a random ice cream with random flavor and random cone.

This is an example of this scenario using just a list and random.choice two times.

import random

flavors = ['vanilla', 'chocolate', 'cookie-dough', 'mint-chocolate', 'rainbow']

cone = random.choice(flavors) + '-cone'
icecream = random.choice(flavors)
crissal
  • 2,547
  • 7
  • 25