select random entry from dictionary

Tony Meyer t-meyer at ihug.co.nz
Mon Mar 7 20:42:56 EST 2005


> How can I select a random entry from a dictionary, regardless of its 
> key-values?

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

(etc)

=Tony.Meyer




More information about the Python-list mailing list