choosing a random element from a dictionary

Jp Calderone exarkun at intarweb.us
Wed Feb 5 19:56:45 EST 2003


On Wed, Feb 05, 2003 at 04:30:27PM -0800, Yuval wrote:
> Is there a function that picks a random element from a dictionary? To
> my understanding the popitem() method does that, but it also removes
> it from the dictionary. Of course I can use popitem() and add it back
> directly, but is there any other way of doing so?
> 

    import random
    random.choice(dict.items())
    random.choice(dict.iteritems())

  Jp

-- 
http://catandgirl.com/view.cgi?44
-- 
 up 52 days, 3:50, 3 users, load average: 0.40, 0.38, 0.32
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 196 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20030205/ae764fcd/attachment.sig>


More information about the Python-list mailing list