473,385 Members | 1,267 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

select random entry from dictionary

Hi

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

regards tores
Jul 18 '05 #1
5 6206
>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))

Jul 18 '05 #2
import random

print d[d.keys()[int(random.random()*len(d.keys()))]]

HTH

-c

Jul 18 '05 #3
ga********@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
Jul 18 '05 #4
Peter,

Agreed... which is why I said the 'random' module should be imported if
more randomness is required.

I only mentioned d.popitem() first in case Tores' application didn't
need a psuedo-random item and instead he was looking to pull any value
without randomness.

Jul 18 '05 #5
>> 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))


Peter> Although this idea may suit the OP, "arbitrary" is most
Peter> definitely not "random".

Correct. The library reference is pretty clear about this too:

Keys and values are listed in an arbitrary order which is non-random,
varies across Python implementations, and depends on the dictionary's
history of insertions and deletions.

(This is from note 3 of http://docs.python.org/lib/typesmapping.html, which
is referenced six times from the operations table on that page.)

It's tempting to think it's random because you see the order change in
seemingly unpredictable ways, but it's definitely not.

Skip
Jul 18 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bart Nessux | last post by:
New to Python... trying to figure out how to count the objects in a list and then map the count to the objects or convert the list to a dict... I think the latter would be better as I need a number...
8
by: kp87 | last post by:
I am a little bit stuck .... I want to play a bunch of soundfiles randomly, but i want to give each soundfile a rating (say 0-100) and have the likelihood that the file be chosen be tied to its...
11
by: Olaf \El Blanco\ | last post by:
How can i generate random words? ('a'..'z') Is there any function that convert a number to it ascci char? My english is horrible! Here an example: function(65) return 'a'; Thank you!
0
by: mj.clift | last post by:
Hi All, Could someone please help me translate my code from Python into C#? I'm a beginner at c# so all help is gratefully appreciated. It starts with a given list (l1) such as . For the...
7
by: Jay | last post by:
How would I be able to grab random words from an internet source. I'd like to grab a random word from a comprehensive internet dictionary. What would be the best source and the best way to go...
39
by: Alan Isaac | last post by:
This may seem very strange, but it is true. If I delete a .pyc file, my program executes with a different state! In a single directory I have module1 and module2. module1 imports random and...
4
by: toglez | last post by:
Hi all! I use PyQt4 and Python to create a program to display numbers from a file in a QTable Widget, select various columns from the table and then display the selected columns in a new table. ...
4
AnuSumesh
by: AnuSumesh | last post by:
Hi I want that if user is logged in on one machine and trying to log in another machine also then he should not be allowed to log in again. For that-- i am creating Dictionary object on...
2
by: blaine | last post by:
Hey everyone, Just a friendly question about an efficient way to do this. I have a graph with nodes and edges (networkx is am amazing library, check it out!). I also have a lookup table with...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.