jeudi 21 novembre 2019

Python get random element from dictionary

I want to get a value "element":"value" from a dictionary in python.

import random

country = {
"Spain":"Madrid",
"UK":"London",
"France":"Paris"
} 

random.choice(country)

It returns me the following :

File "C:\Users\${username}\AppData\Local\Programs\Python\Python37-32\lib\random.py", line 262, in choice
    return seq[i]
KeyError: 10

My aim, is to select a random value and be left with 1 Country - City left in the dictionary.




Aucun commentaire:

Enregistrer un commentaire