how can i randomly choose keys in dictionary

Tony Meyer t-meyer at ihug.co.nz
Thu Feb 17 21:21:21 EST 2005


> Hi,there. How can I choose a key in dictionary randomly?
> 
> Say, random.choice() in lists,

A dictionary's keys() are a list, so you already have the answer:

>>> import random
>>> d = {'a': 1, 'b': 2, 'c': 3}
>>> random.choice(d.keys())

=Tony.Meyer




More information about the Python-list mailing list