Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
listpythonrandom
Assume I have the following list:
foo = ['a', 'b', 'c', 'd', 'e']
What is the simplest way to retrieve an item at random from this list?
Best Answer
Use
random.choice()
:For cryptographically secure random choices (e.g., for generating a passphrase from a wordlist), use
secrets.choice()
:secrets
is new in Python 3.6. On older versions of Python you can use therandom.SystemRandom
class: