[Tutor] How to pull a random element from a list?

ak@silmarill.org ak@silmarill.org
Tue, 24 Jul 2001 18:06:40 -0400


On Tue, Jul 24, 2001 at 03:47:58PM -0600, steve wrote:
> Greetings
> 
> I'm trying to figure out how the heck to pull an element from a list or 
> dictionary at random. I have a list of words and need to be able to pull one 
> at random. (Yet to decide if a list is the right way to do it. Thoughts?)
> 
> I've messed around with my limited knowledge, but haven't been able to figure 
> out how to do it. I was going to use a random number generator to pick which 
> element to retrieve. 
> 
> I am at your mercy oh mighty Pythonistas! HELP!

>>> l = ['word1','word2','blah','drah']
>>> import random
>>> random.choice(l)
'word2'

> 
> -- 
> Steve - Editor - www.formulaoneupdate.com
> --------------------------------
> "Trish, she has a paddle." - Jeff Hardy
> 
> _______________________________________________
> Tutor maillist  -  Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor

-- 
Cymbaline: intelligent learning mp3 player - python, linux, console.
get it at: http://silmarill.org/cymbaline