Ray Vega Ray Vega - 4 months ago 9
Python Question

How do I randomly select an item from a list using Python?

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?

Answer

Use random.choice:

import random

foo = ['a', 'b', 'c', 'd', 'e']
print(random.choice(foo))
Comments