Question

{"raitisu": [{"name": "test"}, {"name": "test1"}]}         
note = {'name': ""+random.choice(notes.keys())+""}

How to chose between test and test1 randomly?

Was it helpful?

Solution

Choose a list of dictionaries at random from notes (using the fact that a dictionary yields its keys when treated as an iterable), then choose a value from the list using the known key "name".

import random
notes = {"raitisu": [{"name": "test"}, {"name": "test1"}],
         "iceyyheart": [{"name": "test"}]}
notes_key = random.choice(notes)
random_dict_list = notes[notes_key]
random_value = random.choice(random_dict_list)["name"]
note = { 'name': random_value }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top