Ray Vega Ray Vega - 1 year ago 120
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 Source

Use random.choice:

import random

foo = ['a', 'b', 'c', 'd', 'e']
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download