You can retrieve an item at random from a list using the random.choice()
function from the random
module. Here's how you can do it:
python
import random
foo = ['a', 'b', 'c', 'd', 'e']
random_item = random.choice(foo)
print(random_item)
In this example, random.choice(foo)
selects a random element from the list foo
and assigns it to the random_item
variable. The selected element will be printed to the console. The random.choice()
function ensures that each item in the list has an equal chance of being chosen.
Comments
Post a Comment