select random entry from dictionary

Peter Hansen peter at engcorp.com
Tue Mar 8 16:17:41 CET 2005


gaudetteje at gmail.com wrote:
>>From the Python 2.4 quickreference:
> 
> d.popitem()  	 Removes and returns an arbitrary (key, value) pair from
> d
> 
> If this isn't random enough, then you can generate a random number in
> range(len(d))

Although this idea may suit the OP, "arbitrary" is
most definitely not "random".  Given the same
dictionary on two separate occasions, this approach
results in precisely the same sequence of items
being returned.

-Peter



More information about the Python-list mailing list